Generics and Type-erasure

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public void doSomething(List<One>);
public void doSomething(List<Two>);
</code>
<code>public void doSomething(List<One>); public void doSomething(List<Two>); </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
}
}
}
</code>
<code>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 } } } </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật