What's wrong with D's templates?

Yigal Chripun yigal100 at gmail.com
Fri Dec 18 00:50:52 PST 2009


On 18/12/2009 02:49, Tim Matthews wrote:
> In a reddit reply: "The concept of templates in D is exactly the same as
> in C++. There are minor technical differences, syntactic differences,
> but it is essentially the same thing. I think that's understandable
> since Digital Mars had a C++ compiler."
>
> http://www.reddit.com/r/programming/comments/af511/ada_programming_generics/c0hcb04?context=3
>
>
> I have never touched ada but I doubt it is really has that much that
> can't be done in D. I thought most (if not all) the problems with C++
> were absent in D as this summary of the most common ones points out
> http://www.digitalmars.com/d/2.0/templates-revisited.html.
>
> Your thoughts?

I don't know Ada but I do agree with that reddit reply about c++ and D 
templates. D provides a better implementation of the exact same design, 
so it does fix many minor issues (implementation bugs). An example of 
this is the foo<bar<Class>> construct that doesn't work because of the 
">>" operator.
However, using the same design obviously doesn't solve any of the deeper 
design problems and this design has many of those. An example of that is 
that templates are compiled as part of the client code. This forces a 
library writer to provide the source code (which might not be acceptable 
in commercial circumstances) but even more frustrating is the fact that 
template compilation bugs will also happen at the client.

There's a whole range of designs for this and related issues and IMO the 
C++ design is by far the worst of them all. not to mention the fact that 
it isn't an orthogonal design (like many other "features" in c++). I'd 
much prefer a true generics design to be separated from compile-time 
execution of code with e.g. CTFE or AST macros, or other designs.



More information about the Digitalmars-d mailing list