I was trying to define a type that accept an existential higher kinded type in Scala.
Unfortunately Scalac does not allow it.
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait H[F[_, _]]
trait T[A, B]
val h:H[T] = null
val e:H[F] forSome { type F[A, B] } = h
// Exiting paste mode, now interpreting.
<console>:13: error: type mismatch;
found : H[T]
required: H[_[A, B] <: Any]
Note: T <: Any, but trait H is invariant in type F.
You may wish to define F as +F instead. (SLS 4.5)
val e:H[F] forSome { type F[A, B] } = h
^
How could I workaround it?
How could I define a type that matches any H
whatever the type parameter is?
Update: I even tried to let Scalac infer the existential type, still not lucky.
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait H[F[_, _]]
trait T[A, B]
val h:H[T] = null
trait T2[A, B]
val h2:H[T2] = null
Seq(h, h2)
// Exiting paste mode, now interpreting.
<console>:17: error: type mismatch;
found : H[T]
required: H[_ >: T2 with T <: Object]
Seq(h, h2)
^
<console>:17: error: type mismatch;
found : H[T2]
required: H[_ >: T2 with T <: Object]
Seq(h, h2)
^