views:

84

answers:

4

I'm becoming increasingly frustrated with the limits of type-erased Java generics. I was wondering if there was a custom Java Compiler that provided a full version of generics without the quirks associated with type-erasure?

Chris

+4  A: 

It is not just a compiler change that would be required. I think it would also be necessary to change the JVM implementation in ways that are incompatible with the JVM spec, and the Java class libraries in ways that are incompatible with the current APIs.

For example, the semantics of the checkcast instruction change significantly, as must the objects returned by the Object.getClass() operation.

In short, the end result would not be "Java" any more and would be of little interest to the vast majority of Java developers. And any code developed using the new tools/JVM/libraries would be tainted.

Now if Sun/Oracle were proposing / making this change ... that would be interesting.

Stephen C
A: 

Question is meaningless unless you are asserting that the JDK compiler doesn't implement the language correctly. Any compiler that didn't obey the same rules wouldn't be a Java compiler so nobody could recommend its use.

EJP
+1  A: 

Scala (a language which runs on top of the JVM) may allow you to get around the problem of type erasure using the powerful concept of manifests, which essentially give you reified types.

More info: http://www.scala-blogs.org/2008/10/manifests-reified-types.html

Chris Dennett
Quoting from the blog. "Scala's support for implicit parameters, and the automatic injection of Manifests by the compiler, makes reifying types on the JVM *slightly less painful*." (my emphasis)
Stephen C
A: 

It would be doable, but I'm not aware of anyone that's done it yet. It would require a significant rewrite of javac to make it instantiate generics when needed (creating a new .class file for each instantiation), but otherwise should be reasonably straight-forward. It could even add support for using primitive types as generic type args.

Chris Dodd