Reified generics in Scala 2.10

Your argument is flawed. Kotlin has not been released yet*, and Ceylon just had its first version released, and I’ll quote one of the things it is missing from their announcement:

  • reified generics

So, excuse me, but what implementation proves it is possible? In fact, I haven’t looked much at what Kotlin is promising, but what Ceylon is promising is just what manifests already provide, but in a transparent manner.

But let’s consider the problem you described in your question:

trait Handles[E <: Event] {
  def handle(event: E)
}

So, first of all, JVM doesn’t provide any way of identifying type parameters in interfaces or classes, so E cannot be checked by JVM. You can, however, store information about what E stands for in each object that implements Handles, just like you could write this in Scala:

abstract class Handles[E <: Event : Manifest] {
  def handle(event: E)
}

Next, let’s see the method handle. Again, JVM provides no way of using type parameters in a method definition. The only way to implement that is to have handle accept Object as parameter: ie, type erasure.

And here’s the deal: to make handle callable from Java, it must be type erased. And, if it is type erased, then it is subject to the limitation described in your question. The only way to get around that is to drop Java compatibility (which, by the way, is not available in Ceylon’s first release either).

Yes, Scala will have reification (of some sort) on 2.10, according to Martin Odersky. But whatever it provides (and I’m betting on more transparent use of manifests to assert type equality), this particular limitation is intrinsic to JVM and cannot be overcome without dropping Java integration.

(*) Kotlin has a demo out now, and its reification — so far — is just a syntactic sugar for bundling manifests and instanceOf tests. It’s still subject to all the same limitations Scala is.

Leave a Comment