Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are type projections useful for?

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?

like image 352
tan-theta Avatar asked Aug 12 '11 20:08

tan-theta


People also ask

What is the purpose of projections?

What Is the Purpose of Projection? Sigmund Freud believed projection to be a defense mechanism often used as a way to avoid uncomfortable repressed feelings. Feelings that are projected may be controlling, jealous, angry, or sexual in nature.

What are different map projections used for?

Different projections have different uses. Some projections are used for navigation, while other projections show better representations of the true relative sizes of continents.

What is the most commonly used projection?

One of the most famous map projections is the Mercator, created by a Flemish cartographer and geographer, Geradus Mercator in 1569. It became the standard map projection for nautical purposes because of its ability to represent lines of constant true direction.


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 as S#A, S#B, and so on. Between two matrices of type Matrix[S], for the same S, these types will be compatible (unlike what would be the case with path dependent types).

like image 168
Kipton Barros Avatar answered Oct 19 '22 13:10

Kipton Barros


One thing type projections can be used for is partial type application:

({type λ[x]=Tuple2[Int,x]})#λ
like image 9
michid Avatar answered Oct 19 '22 11:10

michid