Is it possible to use the context bounds syntax shortcut with higher kinded-types?
trait One { def test[W : ClassManifest]: Unit } // first-order ok
trait Two { def test[W[_]: ClassManifest]: Unit } // not possible??
trait Six { def test[W[_]](implicit m: ClassManifest[W[_]]): Unit } // hmm...
Yes, it is, but your context bound type must have a higher kinded type parameter (which ClassManifest doesn't).
Update
It's possible to use a type alias to allow a higher-kinded type parameter to be bounded by a first-order context bound type. We use the type alias as a type-level function to make a higher-kinded type out of the first-order type. For ClassManifest it could go like this,
Note that on the right hand side of the type alias CC[_] is a first-order type ... the underscore here is the wildcard. Consequently it can be used as the type argument for ClassManifest.
Update
For completeness I should note that the type alias can be inlined using a type lambda,
Note that
implicitly[ClassManifest[List[_]]]
is short forimplicitly[ClassManifest[List[T] forSome {type T}]]
.That's why it works:
ClassManifest
expects a proper type argument, andList[T] forSome {type T}
is a proper type, butList
is a type constructor. (Please see What is a higher kinded type in Scala? for a definition of "proper" etc.)To make both
ClassManifest[List[String]]
andClassManifest[List]
work, we'd need to overloadClassManifest
somehow with versions that take type parameters of varying kinds, something like:(On an academic note, the "proper" way to do this, would be to allow abstracting over kinds:
)