What's wrong with D's templates?

Yigal Chripun yigal100 at gmail.com
Fri Dec 18 14:24:50 PST 2009


On 18/12/2009 16:02, retard wrote:
> Fri, 18 Dec 2009 08:53:33 -0500, bearophile wrote:
>
>> Yigal Chripun:
>>> 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.
>>
>> My creativity is probably limited, so I think that while C++/D templates
>> have some well known problems, they are better than the strategies used
>> by Java, C#, Ada, Haskell, Object-C, Scala, and Delphi to define generic
>> code. They produce efficient code when you don't have a virtual machine
>> at run time, and allow to write STL-like algorithms. If you need less
>> performance and/or you accept worse algorithms/collections then I agree
>> there are designs simpler to use and cleaner than C++/D templates. If
>> you are able to design something better I'd like to know about your
>> ideas.
>
> Templates are good for parameterizing algorithms and data structures.
> They begin to have problems when they are used extensively for meta-
> programming. For instance the lack of lazy evalution in the type world
> forces the language to either have 'static if' or you need to add
> indirection via dummy members. The language is basically purely
> functional, but it's several orders of magnitude more verbose than say
> Haskell.
>
> CTFE solves some of the problems, but as a result the system becomes
> really unorthogonal. Macros on the other hand solve the problem of clean
> meta-programmming but are not the best way to describe generic types.
>
> Java, C#, Scala, Haskell et al only support types as template parameters.
> In addition Java erases this type info on runtime so you get even worse
> performance than on C#/.NET.

To bearophile:
you're mistaken on all counts - generics (when properly implemented) 
will provide the same performance as templates. Also, a VM is completely 
orthogonal to this. Ada ain't VM based, is it?

to retard:
different problems should be solved with different tools. Macros should 
be used for meta-programming and generics for type-parameters. they 
don't exclude each other. E.g. Nemerle has an awesome macro system yet 
it also has .net generics too.
As the saying goes -"when all you got is a hammer everything looks like 
a nail" which is a very bad situation to be in.
templates are that hammer while a much better approach is to go and by a 
toolbox with appropriate tools for your problem set.



More information about the Digitalmars-d mailing list