What are type projections in Scala useful for? Why does Scala's type system support both type projections and path dependent types? What was the rationale behind this design decision?
2 Answers
Not a complete answer, but here are some uses for type projections that I have encountered:
Type level metaprogramming. For examples, see Michid's series (parts I, II, III), Jesper's implementation of HList, and the series at Apocalisp.
A workaround to enable type inference (for examples, here are some previous SO questions 1, 2, 3).
A way to bundle a bunch of types into a single type parameter. For example, in a matrix library I'm developing, I define
trait Scalar { type A; type B; type C; ... }
and then pass it as a single parameter to my matrix trait,trait Matrix[S <: Scalar] ...
The individual types can be referred to asS#A
,S#B
, and so on. Between two matrices of typeMatrix[S]
, for the sameS
, these types will be compatible (unlike what would be the case with path dependent types).

- 1
- 1

- 21,002
- 4
- 67
- 80
-
Jesper's post you link states that the code doesn't work, but later post explain that Scala's newer version supports it: http://jnordenberg.blogspot.com/2009/09/type-lists-and-heterogeneously-typed.html – Blaisorblade Sep 02 '11 at 20:54
One thing type projections can be used for is partial type application:
({type λ[x]=Tuple2[Int,x]})#λ

- 10,536
- 3
- 32
- 59