Walter Bright:
> With enough indirection, template instances can all be done with one function
> regardless of the actual argument types. What is lost with that approach,
> however, are all the benefits of inlining, constant folding, specialization,
> etc
My idea 5 was not to remove templates from D, and not even to add normal
Java-like generics to D. And I know templates are usually faster and more
flexible.
My idea was to create something mixed, where you can choose what input
arguments are boxed as in Java, and what ones are not boxed as in D/C++. So the
purpose was not to replace normal D templates, but to add flexibility. Because
I think there are templates that don't need max performance, in such case using
a mixed template-generic (or full generic) can give a "good enough" performance
while keeping the final binary smaller. Reducing binary size is good because it
decreases the pressure on the code L1 cache, and this speeds up the program a
little.
So it's like a cross between a template and a generic.
This is a normal D template:
T1 foo(T1, T2, T3)(T1 x, T2, y, T3 z) {...}
This is a full java-style generic, that will have only one implementation in
the binary, x, y and z will be boxed inside foo:
@generic T1 foo(@generic T1, @generic T2, @generic T3)(T1 x, T2, y, T3 z) {...}
And this is something mixed, here T1 and T3 will be boxed but T2 will not, T2
is like a D template argument, so the compiler will instantiate as many
different T2 it will find:
@generic T1 foo(@generic T1, T2, @generic T3)(T1 x, T2, y, T3 z) {...}
>Generic types can also be more bloated because they have to carry around the
>indirect references, while templated types can specialize them away.<
You are right, as always in life it's a matter of compromises and balances :-)
If you instantiate a hybrid template like foo(@generic T1, T2, @generic T3)
like with ten different T2 types, you can usually produce more code than a
single (probably bigger) 100% generic like @generic T1 foo(@generic T1,
@generic T2, @generic T3) :-)
Bye,
bearophile