The lack of reified generics in Scala is the thing that bugs me the most about the language, since simple things cannot be implemented without using complicated constructs.

Both Kotlin and Ceylon supports reified generics so its definitely possible to do so on top of the JVM. In the past it was said that Scala could not support them without a change in the JVM, but now Scala 2.10 is rumored to have limited support for reification. So my question is:

What can we expect for reification in Scala 2.10, will I for example be able to implement a generic trait multiple times ?. Just how limited is it ?

If Scala 2.10's reification turns out to be more limited than Kotlin and Ceylon. Why is that ?

I tried updating the question so it's now hopefully simpler to answer (as in Yes/No you cannot implement a generic trait multiple times in Scala 2.10 as the type reification will require name mangling in the generated classes, making them complicated to use in Java. Or whatever the answer might be.
– Lars TackmannDec 22 '11 at 15:02

4

Odersky said reification is in 2.10, so it's more than a rumor.
– MalvolioDec 22 '11 at 16:48

2

You mean the non-existing Kotlin and the Ceylon-that-was-just-released-without-reification shows that reified generics is possible? How does that work?
– Daniel C. SobralDec 22 '11 at 19:11

13

I cannot understand how this question can ever be considered "not constructive". The question is very objective and has clear answers, despite the reference to languages/features not yet available.
– Daniel C. SobralDec 22 '11 at 21:02

3 Answers
3

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 Ecannot 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:

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.

I assumed that since the specs mentioned reification (fx. goo.gl/jMvzG) that the authors have indeed found a way to do it. If not then you are of cause completely correct. My question was more on the nature of generics in 2.10 and why it is that kotlin and ceylon can solve this problem and Scala cannot (assuming they can).
– Lars TackmannDec 22 '11 at 20:37

4

@Lars: No, there is no way to do proper reified generics on the JVM, without creating a completely new abstraction between the JVM, class-files, bytecode and the language. Despite the fact that two new languages claim to support reified generics, those languages have delivered nothing to this day except some prettified instanceOf tests.
– socJan 12 '12 at 18:27

1

Gosu supports reified generics, and even though Kotlin and Ceylon don't have releases out that support them, there is no doubt in my mind they will as well. Having said that, I think adding reified generics in Scala is a mistake that will take a toll on the language, while needing access to generic information is pretty rare and usually met adequately with manifests. Gosu link: gosu-lang.org/doc/Gosu%20Reference%20Guide/wwhelp/wwhimpl/…
– Cedric BeustJan 13 '12 at 5:28

4

Although I know that no facts on this planet can change your one-and-true-opinion, did you even actually check your claims for a microsecond? :-) Just one example "Errors: A method with the same name reifies to the same signature at the bytecode level. Rename one of the methods.". Notice how conveniently "erases" got replaced by "reified"? I could go on if the compiler wouldn't be completely unstable and crash when trying to implement a simple interface ("java.lang.ClassFormatError: Illegal field modifiers in class program_/__Program__689").
– socJan 13 '12 at 7:09

2

A one-line update note at the top, would have been as fast as the comment you just left.
– Jayson MinardJan 4 '16 at 19:45

@Ollip your answer should be updated, Kotlin does have reified generics for inline functions which are used heavily and solve most common cases. Your answer is now misleading for current Kotlin.
– Jayson MinardDec 29 '15 at 4:27