In Scala, why can’t I use a context bound for type constructors? [duplicate]
Possible Duplicate:
Context bounds shortcut with higher kinded-types
Why doesn't the Scala compiler let me write this?
class TypeCtor[M[_]: ClassManifest]
It complains with “error: type M takes type parameters”. If I'm only asking for the ClassManifest
, the compiler should be able to insert it no matter what the parametrization of M
is, no?
This works as expected:
class TypeCtor[M[_]](implicit val ev: ClassManifest[M[_]])
(new TypeCtor[Vector]).ev.erasure // => class scala.collection.immu开发者_如何学Pythontable.Vector
See my answer to this question.
The parameterization of M is indeeed irrelevant, but the Scala compiler needs a parameter for ClassManifest in order to know which manifest to insert. Let's say we have
class TypeCtor[A,B](implicit val ev: ClassManifest)
Now the Compiler would not know whether to insert the Manifest of A or B. Also you can't write ClassManifest[M] because ClassManifest expects a Type of kind * and M has kind * -> *.
精彩评论