Compile-time exceptions

Christopher Wright dhasenan at gmail.com
Tue Nov 25 17:12:55 PST 2008


bearophile wrote:
> Christopher Wright Wrote:
>> I don't know what this is doing. That code fragment won't compile, and 
>> it isn't showing usage.
> 
> Sorry:
> 
> struct _add { T opCall(T.init + T.init)(T x) { return x+x; } }
> _add add;
> 
> Now you can give it to a function, without the need of specializing it for a type T first (you can't give the pointer to a template). (This may also be faster, because there isn't a delegate to call).
> (Haskell is statically typed and allows you do more complex things with a clean enough syntax).

I thought you could do this with an alias parameter, but perhaps I'm 
wrong. It's not the sort of thing I try to do, usually.

>> Virtual templates would allow you to use polymorphism in this case, but 
>> that's not going to be considered for quite some time, if at all.
> 
> Can you explain?

Like generics in C#. Java's just hiding casts with its generics, but C# 
does specialized code generation at runtime for each generic type it 
creates. D could do this with full templates, not just generics, and at 
compile time, but it'd require an extra compilation step before linking, 
most likely.

> Bye,
> bearophile



More information about the Digitalmars-d mailing list