Questions tagged [type-projection]

In the Scala language, a type projection `T#x` references the type member named `x` of type `T`.

According to §3.2.2 of the Scala language specification, a type projection T#x references the type member named x of type T. For example if there was a trait Animal { type Food }, then it is possible to refer to the type member Food using the projection Animal#Food. Unlike path-dependent types, this projection describes the Food type for any type of Animal, not a particular class or instance of Animal.

31 questions
13
votes
1 answer

Why can’t Scala infer the path of a path-dependent type—even from an explicit self-reference?

I'd like to pass an object to a function that accepts an argument with a projected type, and get Scala to deduce that the object's type comes from the object that encloses it. Here's some simple code to illustrate the difficulty: trait Cult { cult_…
Ben Kovitz
  • 4,279
  • 1
  • 17
  • 40
11
votes
2 answers

How to infer the right type parameter from a projection type?

I have some troubles having Scala to infer the right type from a type projection. Consider the following: trait Foo { type X } trait Bar extends Foo { type X = String } def baz[F <: Foo](x: F#X): Unit = ??? Then the following compiles…
betehess
  • 817
  • 4
  • 18
11
votes
2 answers

investigation of `type` and `#` keywords in scala

Could someone explain how the type keyword and # operator works in scala and how to use it? Please look at examples. //Example1 scala> type t1 = Option.type defined type alias t1 //Shouldn't this work since previous example simply works? scala> …
pawel.panasewicz
  • 1,769
  • 15
  • 25
8
votes
2 answers

Cannot implement representation type as type member

While cracking my head over another question, I came across different riddles which seem related. This is one of them: trait Sys[S <: Sys[S]] { type Peer <: Sys[Peer] } trait Fenced { type Peer <: Sys[Peer] } def makeFence[S <: Sys[S]] = new…
0__
  • 64,257
  • 16
  • 158
  • 253
5
votes
1 answer

Scala type projection with higher-kinded type

Consider the following: trait Foo { type F[_] type A type FA = F[A] def get: FA } class SeqStringFoo extends Foo { type F[_] = Seq[_] type A = String def get: Seq[String] = Seq("hello world") } def exec[F <: Foo](foo: F): F#FA =…
Daniel Shin
  • 4,246
  • 25
  • 47
4
votes
1 answer

What does Dotty offer to replace type projections?

I have been reading about Dotty, since it looks like it is about to become scala 3, and noticed that type projections are deemed "unsound" and removed from the language ... This seems like a bummer, as I have seen several use cases where they were…
Dima
  • 33,157
  • 5
  • 34
  • 54
4
votes
1 answer

Type mismatch with type projection

I'm having a compiler type mismatch error that I do not understand. Given the following definition of an Elem and a factory (Companion): object Elem { trait Companion[E[~] <: Elem[~]] { def apply[S](peer: E[S]#Peer): E[S] // wrap a peer in its…
0__
  • 64,257
  • 16
  • 158
  • 253
3
votes
3 answers

Why do these type arguments not conform to a type refinement?

Why does this Scala code fail to typecheck? trait T { type A } trait GenFoo[A0, S <: T { type A = A0 }] trait Foo[S <: T] extends GenFoo[S#A, S] I don't understand why "type arguments [S#A,S] do not conform to trait GenFoo's type parameter bounds…
Kipton Barros
  • 20,144
  • 3
  • 63
  • 77
3
votes
3 answers

Compilation error when declaring Functor for Either even after using type projections

I am trying to write a Functor for Either for academic purposes in Scala. With help of higher-kinded types and type-projections, I managed to write an implementation for Either. trait Functor[F[_]] { def map[A, B](fa: F[A])(f: A => B):…
3
votes
1 answer

Scala type projection: pick an object within a path-dependent trait

I have a path-dependent trait declaring several modules. That is fine. However, I write a macro and I need to access those inner types and I am unable to write a proper expression selecting them. trait A { type Foo object module { // one of…
Gaim
  • 6,289
  • 4
  • 35
  • 58
3
votes
2 answers

Referring abstract type member of a type parameter

My scenario is like: trait A { type B def foo(b: B) } trait C[D <: A] { val d: D def createB(): D#B def bar() { d.foo(createB) } } In REPL, it complains :24: error: type mismatch; found : D#B required:…
Chikei
  • 2,064
  • 1
  • 17
  • 20
3
votes
4 answers

F-bounded quantification through type member instead of type parameter?

I would like to move a type parameter to a type member. This is the starting point which works: trait Sys[S <: Sys[S]] { type Tx type Id <: Identifier[S#Tx] } trait Identifier[Tx] { def dispose()(implicit tx: Tx): Unit } trait Test[S <:…
0__
  • 64,257
  • 16
  • 158
  • 253
2
votes
2 answers

Dotty cannot infer result type of generic Scala function taking type parameter trait with abstract type

A simple value hierarchy Imagine this simple trait Value where every implementing class has a value of some type T. trait Value { type T def value: T } We have two different implementing classes representing Int and String values…
mgd
  • 3,484
  • 3
  • 19
  • 29
2
votes
1 answer

Scala: Returning a dependent type

In my application, I have a closed set of operations that return a corresponding set responses as shown below. sealed trait OperationCompletionResponse { val state: Int } case class ExecutionStartedResponse(state: Int) extends…
2
votes
2 answers

What is the correct restriction on a type projection in Scala?

I have a difficulty with restrictions on type projections in Scala. Assume I have the following code: sealed trait Color case object Red extends Color case object Green extends Color case object Blue extends Color trait Item { type Colors <:…
Max
  • 45
  • 6
1
2 3