Template constraints in D

janderson askme at me.com
Sat Jun 21 10:32:53 PDT 2008


bearophile wrote:
> Walter Bright:
>> http://www.digitalmars.com/d/2.0/concepts.html
> 
> They look nice (but I may use a bigger list of examples to understand how they can be used). What can they do that can't be done with the things already present?
> 
> bool isprime(int n) { ... }
> template Foo(int N) {
>     static assert (isprime(N), "error");
>     ...
> }
> 
> 
> template Foo(int N) {
>     static if (N & 1) {
>         // A code
>     } else {
>         // B code
> }
> Foo!(3)    // instantiates Foo with A code
> Foo!(64)   // instantiates Foo with B code
> 
> Bye,
> bearophile

For one thing they get away from the big-long-if-statements.  That means 
you can extend a template in a different place, so the code is less brittle.

-Joel



More information about the Digitalmars-d mailing list