[typing] Type-erasure re generics

Jacob Carlborg doob at me.com
Wed Sep 29 09:04:59 PDT 2010


On 2010-09-29 17:14, Justin Johansson wrote:
> Sorry if I should ask to this on D.help.
>
> Can someone please inform me if D does type-erasure on generic
> types as does Java?
>
> Here's one Java reference on the subject that I found.
> http://download.oracle.com/javase/tutorial/java/generics/erasure.html
>
> 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.
>
> Thanks for answers,
>
> -- Justin Johansson

D does not perform type-erasure. It creates a new version of every 
template for every type that is used with that template. For example:

T add (T) (T x, T y) { return x + y; }

add(3, 4)
add(4.0, 5.0);

The above template will generate two instances, like this:

int add (int x, int y) { return x + y; }
double add (double x, double y) { return x + y; }

If we look at D's templates:
pro:
* stronger type system

con:
* code bloat
* perhaps longer compile time

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list