Understanding Type Projection
Asked Answered
P

1

6

Taken from typelevel/kind-projector, what's the distinction between:

// partially-applied type named "IntOrA"
type IntOrA[A] = Either[Int, A]

and

// type projection implementing the same type anonymously (without a name).
({type L[A] = Either[Int, A]})#L

?

Are they equivalent?

Pectinate answered 3/3, 2016 at 19:44 Comment(5)
What is this #L at the end?Dunkirk
@YuvalItzchakov well #L accesses the type member that was just created insideHonour
@Łukasz And what's the use of #?Dunkirk
@YuvalItzchakov # is type projection. It allows you to access any path dependent type, which L is and treat it as not being path dependent, i.e. by path dependent types inner types are not equal when accessed with ., but are equal when accessed with #. See more #9443504Ind
Related question What is a kind projectorWiskind
V
4

They are almost equivalent, as it is said in the comment.

Say you have a class trait Super[F[_]] {}, and you want to implement it where F[x] = Either[Int, x] You could write:

type IntOrA[A] = Either[Int, A]
class B extends Super[IntOrA] {}

But if you want a one liner, you could write:

class B extends Super[({type L[A] = Either[Int, A]})#L] {}

Or with kind-projector you could write it like:

class B extends Super[λ(A => Either[Int, A])] {}

or even:

class B extends Super[Either[Int, ?]] {}

there is no other difference than making it a one line and having this type anonymous.

Visa answered 3/3, 2016 at 22:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.