Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any plans for the JVM to support generics at runtime?

Tags:

java

generics

You know, like the CLR does. Is anyone even admitting the lack of runtime generic information is a problem, and working to solve it?

like image 741
ripper234 Avatar asked Oct 22 '10 15:10

ripper234


2 Answers

The designers of Java opted for this solution to maintain backward compatibility (on the bytecode level). Since then, there is even more Java code out there, thus breaking backward compatibility would have ever worse consequences. So I doubt they would change their minds about this.

like image 172
Péter Török Avatar answered Oct 16 '22 11:10

Péter Török


Why is this a problem (from the comments to your question)

Well, consider for instance that can't overload a method like this:

void foo(List<String> strings) { ... }
void foo(List<Integer> ints) { ... }

even though it makes sense to actually allow this. (The main reason why this doesn't work today, is because it would, when compiled to bytecode, look like foo(List strings) and foo(List ints).)

Are there any plans for the JVM to support generics at runtime?

According to this page it's at least not in the pipe-line for Java 7:

Reified Generics

Currently, generics are implemented using erasure, which means that the generic type information is not available at runtime, which makes some kind of code hard to write. Generics were implemented this way to support backwards compatibility with older non-generic code. Reified generics would make the generic type information available at runtime, which would break legacy non-generic code. However, Neal Gafter has proposed making types reifiable only if specified, so as to not break backward compatibility.

Links

  • Neal Gafter's blog: Reified Generics for Java
  • Two problems with generics in Java
like image 38
aioobe Avatar answered Oct 16 '22 11:10

aioobe