Templates everywhere

Walter Bright newshound1 at digitalmars.com
Sun Mar 14 13:29:27 PDT 2010


Philippe Sigaud wrote:
> 1- thanks Walter for opening new vistas to me. Doing templated code
> in D is so fun and powerful that you transformed forever the way I
> look at problems.

Years ago, although I had implemented templates for C++, I didn't "get" 
them. Not at all. I didn't even plan to put templates in D, because they 
seemed overly complex, ugly, and had little value.

It was pretty obvious, though, that the C++ community did get templates 
and did find them very useful. Something was wrong with my perception. I 
eventually realized that what was preventing me from understanding C++ 
templates was the syntax. Change the syntax, and what is happening 
suddenly becomes obvious.

======= C++ =========
template<class T> class Foo
{
     T member;
};
======= D ===========
class Foo(T)
{
     T member;
}
=====================

Yes, it looks like a trivial change, and perhaps it is. But it makes a 
world of difference, because it now looks like something I do get - a 
parameter list to something.

I had another epiphany from watching Scott Meyers give a presentation 
about an application for templated type lists in C++. Nearly the full 
hour was spent detailing a titanic struggle with the syntax. I suddenly 
realized that the whole presentation would have been over in 5 minutes 
if the syntax of type lists was simply that of an array of types. We 
already understand arrays of values, so why should an array of types be 
any different?

It isn't the concept or application of templates that is hard. It's the 
C++ syntax for them that's hard. It's hard in C++ because when they were 
designed, little was known or understood about what templates were good 
for and how they should be used. Now that we do know, we can make a much 
more straightforward design.



More information about the Digitalmars-d mailing list