Why does D not have generics?
jmh530
john.michael.hall at gmail.com
Tue Jan 12 15:24:19 UTC 2021
On Tuesday, 12 January 2021 at 14:45:36 UTC, Adam D. Ruppe wrote:
> [snip]
>
> Yeah, in the D context since we obviously have generic
> templates, I'd take it to mean the Java style.
>
> 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.
> [snip]
Not just classes? Could you explain a bit more about what you are
thinking in that paragraph? If it's not a class, then are you
talking about a struct? As in something like below. Does the same
approach work when it's not a class?
generic Number(T)
if (isNumber!T)
{
struct Number {
T x;
}
}
More information about the Digitalmars-d
mailing list