What's wrong with D's templates?

dsimcha dsimcha at yahoo.com
Fri Dec 18 07:34:51 PST 2009


== Quote from Yigal Chripun (yigal100 at gmail.com)'s article
> 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).

I think variadics, static if and alias parameters qualify more as a "better
design" than fixing "minor issues".

> 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)

There's always obfuscation.  I think people underestimate this.  If you strip out
all comments and meaningful names (I won't mention mangling whitespace because
that's programmatically reversible), trade secrets are reasonably well protected.
 Remember, people can always disassemble your library.  I don't see obfuscated D
code with names and comments stripped as being **that** much easier to understand
than a disassembly.

> 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.

Since generics work by basically casting stuff to Object (possibly boxing it) and
casting back, I wonder if it would be easy to implement generics on top of
templates through a minimal wrapper.  The main uses for this would be executable
bloat (for those that insist that this matters in practice) and allowing virtual
functions where templates can't be virtual.



More information about the Digitalmars-d mailing list