What's wrong with D's templates?

BCS none at anon.com
Sat Dec 19 17:11:13 PST 2009


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.





More information about the Digitalmars-d mailing list