2

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

Chris
  • 4,450
  • 3
  • 38
  • 49
  • 7
    How could such a thing possibly be implemented as a *library*? I don't see how you could avoid modifying the compiler and the VM. – sepp2k May 24 '10 at 00:41
  • Post what would you like to do and what you have done so far and perhaps we could help further. It is possible to go around the type erasure in most cases. – Ricardo Marimon May 24 '10 at 01:26

4 Answers4

5

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
  • 698,415
  • 94
  • 811
  • 1,216
1

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

Community
  • 1
  • 1
Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
  • 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 May 25 '10 at 00:00
0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

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
  • 119,907
  • 13
  • 134
  • 226