[typing] Type-erasure re generics

Jesse Phillips jessekphillips+D at gmail.com
Wed Sep 29 08:53:55 PDT 2010


Justin Johansson Wrote:

> Also I would like to be informed as to the positives and negatives
> aspects of type-erasure as might be appropriate in a D versus Java
> context.

Here is a quote that is most relevant:

"[Type-erasure is] a process where the compiler removes all information related to type parameters and type arguments within a class or method. Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics."

The answer is, D does not do this. The reason is that it does not need to be backwards compatible with Java libraries written prior to generics...

There really isn't any benefit to this, the point of types is so the compiler can check that you are only doing valid operations on it. If you start removing type information you might as well just be working with Object and leave it at that (actually use variant, I think it is more type safe.)

The only benefit, which should be solve in another manner is having this code work:

class A {}
class B:A {}

class Container(T) {}

void main() {
    Container!(A) a = new Container!(B)();
}

I think this was reported as a bug, but can't seem to find it now.


More information about the Digitalmars-d mailing list