Last Updated: February 25, 2016
·
880
· jroesch

Existential Type Encoding in Scala

We can write an existential explicitly like this in Scala:

type Exists[P[_]] = P[T] forSome { type T }

Now let's introduce a type lambda:

({ type λ[A] = AnotherType[A] })#λ 

The type lambda declares a module inline with a single type member, we then use type projection to extract the unapplied parametrized type. The type is of the kind * -> *, which means we can pass it to Exists, which expects something of kind * -> *.

trait SomeType[A, B]
class HiddenType[A](x: Exists[({ type λ[B] = SomeType[A, B] })#λ])

I figured out this is an interesting way to hide a type, it works very well if you want Haskell style existentials in your constructors.