Generics - the path not taken

T

Tim Tyler

A while ago, Dale King asked why we have type erasure.

Erasure would make sense if Java 1.5 code had to run of earlier JVMs,
but - as has been noted - code compiled for Java 1.5 rarely works
at all on earlier JVMs - and can't be easily targetted at them.

The "Java Theory and Practice: Generics Gotchas" page suggests a reason:

``Extending the virtual machine instruction set to support generics was
deemed unacceptable, because that might make it prohibitively difficult
for JVM vendors to update their JVM.''

- http://www.developertutorials.com/print/125.html
 
R

Roedy Green

``Extending the virtual machine instruction set to support generics was
deemed unacceptable, because that might make it prohibitively difficult
for JVM vendors to update their JVM.''

If they had NOT done type erasure, then serialised streams would
contain the type info. What else would be different?
 
T

Tim Tyler

Roedy Green said:
If they had NOT done type erasure, then serialised streams would
contain the type info. What else would be different?

Lots of things:

You would no longer get error messages about types having the same
erasre.

You could do things like implement Comparable<String> *and*
Comparable<Integer>.

To quote further from the article:

``Because the runtime cannot tell a List<String> from a List<Integer> (at
runtime, they're both just Lists), constructing variables whose type is
identified by a generic type parameter is problematic. This lack of type
information at runtime poses a problem for generic container classes and
for generic classes that want to make defensive copies.''

- http://www.developertutorials.com/tutorials/java/java-theory-generic-050323/page3.html
 
D

Dale King

Tim said:
A while ago, Dale King asked why we have type erasure.

Erasure would make sense if Java 1.5 code had to run of earlier JVMs,
but - as has been noted - code compiled for Java 1.5 rarely works
at all on earlier JVMs - and can't be easily targetted at them.

The "Java Theory and Practice: Generics Gotchas" page suggests a reason:

``Extending the virtual machine instruction set to support generics was
deemed unacceptable, because that might make it prohibitively difficult
for JVM vendors to update their JVM.''

- http://www.developertutorials.com/print/125.html

So basically they are saying that it is acceptable for the VM
implementer to have to make a change to support a different class
version number and to support some of the new 1.5 features like
annotations, but a major change for generics was to great of a change.

I disagree with the conclusion, but understand the logic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top