Why does D not have generics?

Elronnd elronnd at elronnd.net
Wed Jan 13 08:19:25 UTC 2021


On Tuesday, 12 January 2021 at 14:45:36 UTC, Adam D. Ruppe wrote:
> A Java generic doesn't generate new code for other types at 
> all. It is just a runtime class that works in terms of 
> interfaces, and when the compiler parameterizes it, it 
> basically just inserts static casts at the interface boundary 
> for you.
>
> The benefit of this is you have almost zero compile time cost 
> and runtime cost comparable to any other class. It avoids 
> template bloat in codegen that can be very significant in D.
>
> I'd love to have it as an option in D as well. There's a lot of 
> types that can use identical runtime code and just change 
> types. Not just classes, but like integer types too can be 
> identical and merged, const/immutable/shared/etc can be 
> identical and merged, and even other things with cast(void*) 
> can do it.

There was a recent paper about doing this kind of transformation 
automatically as an optimization - 
https://www.microsoft.com/en-us/research/uploads/prod/2020/03/kacc.pdf


More information about the Digitalmars-d mailing list