What's wrong with D's templates?

Yigal Chripun yigal100 at gmail.com
Mon Dec 21 10:12:36 PST 2009


On 20/12/2009 03:11, BCS wrote:
> Hello Yigal,
>
>> On 18/12/2009 17:34, dsimcha wrote:
>>
>>> I think variadics, static if and alias parameters qualify more as a
>>> "better design" than fixing "minor issues".
>>>
>> actually they qualify as - "even worse design". duplicating the syntax
>> like that is butt ugly.
>>
>
> I for one think that it's a better design than C++ has. (Given that 99%
> of what they do, C++ was never designed to do at all, you'd be hard
> pressed to come up with a worse design without trying to.)
>
> If you can come up with an even better design for compile time stuff,
> I'd be interested.
>
>> the conflation of user-code and library code.
>
> Could you elaborate on this?
>
>>
>>>> but even more frustrating is the fact that
>>>> template compilation bugs will also happen at the client.
>
> Jumping back a bit; which client? The one with the compiler or the end
> user?
>
> If the first; removing this puts major limits on what can be done
> because you can't do anything unless you be sure it will work with the
> open set of types that could be instanceiated, including ones you don't
> know about yet. I know some system like c# requiter you to define what
> you will do to a type at the top and then enforce that. IMHO this is a
> non-solution. Without be to silly I think I could come up with a library
> that would requiter a solution to the halting problem in order to check
> that the template code can't generate and error with the given constants
> and that a given type fits the constraints, both without actuality
> instanceate the template for the type.
>
> If the second; nether D nor C++ have to worry about that.
>
>> .Net generics for example work by creating an instantiation for each
>> value type (same as c++ templates) and one instantiation for all
>> reference types since at the binary level all reference types are
>> simply
>> addresses.
>
> I don't know how it does it but there has to be more to it in C# because
> they allow you to do thing to the objects that Object doesn't support.
> For that to happen, the objects have to be wrapped or tagged or
> something so that the generics code can make "foo.MyFunction()" work for
> different types. If I has to guess, I'd guess it's done via a vtable
> either as a "magic" interface or as a fat pointer.
>
> Oh, and if the above is garbage because C# can't access totally
> independent methods from a generic, then right there is my next argument
> against generics.
>
>

What you're talking about in the above is meta-programing. Doing 
meta-programing a-la c++ templates is IMO like trying to use square 
wheels, it is just wrong.

To answer your questions:
D already has better designed tools for this and they keep improving. 
Don is doing an excellent job in fixing CTFE.
I think D needs to go beyond just constant-folding (CTFE) and allow to 
run any function at compile-time in the same manner it's done in Nemerle 
(multi-stage compilation).

This is orthogonal to generics. the limitations you see in C# are *not* 
limitations of its generics but rather of the meta-programing facilities.



More information about the Digitalmars-d mailing list