Generics in Java are implemented using type erasure. The JLS says that the inspiration was backward compatibility. Where as on the other hand C# generics are reifiable.
Theoretically what are the advantages and disadvantages by having Generics as “erasure” or “reifiable”?
Is Java missing onto something?
Well, the motivation (backwards compatibility) is both an advantage and a disadvantage. It is disadvantage because we all would prefer to have reifiable types, but the price to pay was high. Consider the design choices in C#. They have reifiable types, but now they have duplicate APIs. So, imagine a Java API where we also had duplicate APIs for every parameterized class. Now, picture yourself porting thousands of lines of code from legacy classes to the new generic classes. Now, who would not consider duplicate APIs a disadvantage? But hey, they have reifiable types!
So, the major motivation was “evolution, not revolution”. And logically, every decision has tradeoffs.
Besides the other disadvantages mentioned, we could also add the fact that type erasure can be difficult to reason about at compile time, since it is not evident that certain types will be removed, and this leads to very weird and hard to find errors.
The existence of bridge methods (this compiler syntactically-generated methods to keep binary compatibility) can also be seen as disadvantage. And these can be precisely one of the reasons of errors I mentioned in the previous paragraph.
Major disadvantages derive from the already obvious fact that there is a single class and not multiple classes for generic types. As another example consider that overloading a method with same generic class fails in Java:
public void doSomething(List<One>);
public void doSomething(List<Two>);
Something that could be seen as a disadvantage of reifiable types (at least in C#) is the fact that they cause code explosion. For instance List<int>
is one class, and a List<double>
is another totally different, as it is a List<string>
and a List<MyType>
. So classes have to be defined at runtime, causing an explosion of classes and consuming valuable resources while they are being generated.
Regarding the fact that it is not possible to define a new T()
in Java, mentioned in another answer, it is also interesting to consider that this is not only a matter of type erasure. It also requires the existence of a default constructor, that is why C# requires a “new constraint” for this. (See Why new T() is not possible in Java, by Alex Buckley).
2
The downside of type erasure is that you do not know at runtime the type of the generic. This implies that you cannot apply reflection on them and you cannot instantiate them at runtime.
You cannot do something like this in Java:
public class MyClass<T> {
private T t;
//more methods
public void myMethod() {
//more code
if (someCondition) {
t = new T();//illegal
} else {
T[] array = new T[];//illegal
}
}
}
There is a workaround for this but it requires more code. The advantage, as you have mentioned it, is backwards compatibility.
Another advantage of erased generics, is that different languages that compile to the JVM employ different strategies for generics, e.g. Scala’s definition-site versus Java’s use-site covariance. Also Scala’s higher-kinded generics would have been more difficult to support on .Net’s reified types, because essentially Scala on .Net ignored C#’s incompatible reification format. If we had reified generics in the JVM, most likely those reified generics wouldn’t be suitable for the features we really like about Scala, and we’d be stuck with something suboptimal. Quoting from Ola Bini’s blog,
What this all means is that if you want to add reified generics to the
JVM, you should be very certain that that implementation can encompass
both all static languages that want to do innovation in their own
version of generics, and all dynamic languages that want to create a
good implementation and a nice interfacing facility with Java
libraries. Because if you add reified generics that doesn’t fulfill
these criteria, you will stifle innovation and make it that much
harder to use the JVM as a multi language VM.
Personally I don’t consider the necessity of using a TypeTag
context bound in Scala for conflicting overloaded methods as a disadvantage, because it moves the overhead and inflexibility of reification from a global (whole program and all possible languages) to a use-site per language issue that is only the case less frequently.
2