How does D's templated functions implementation differ from generics in C#/Java?

Jacob Carlborg doob at
Sat Aug 8 08:14:38 UTC 2020

On 2020-08-07 23:39, H. S. Teoh wrote:

> They are *very* different.
> Java generics are based on "type erasure", i.e., at the syntactic level,
> containers are parametrized with the element types, but at the
> implementation level, the element types are merely "erased" and replaced
> with Object (a top type of sorts). There is only one container
> instantiation, which is shared across all parametrizations.  I don't
> know exactly why this approach was chosen

I'm guessing a strong reason was to be backwards compatible with earlier 
versions of Java.

The type erasure is also most likely the reason why Java generic types 
don't accept primitive types. They need to be boxed, i.e. `int` need to 
be wrapped in an object of type `Integer`.

/Jacob Carlborg

More information about the Digitalmars-d-learn mailing list