Slides from my ACCU Silicon Valley talk

bearophile bearophileHUGS at lycos.com
Tue Dec 14 00:17:46 PST 2010


Andrei:

>D's constrained templates were introduced on 17 June 2008 and they've been a smashing hit. Virtually all of Phobos uses them, and I'm sure a lot of generic client code. They take half a minute to explain and solve a difficult problem in a very simple manner.<

A template with two constrains makes it less handy to add error messages that explain why the instantiation has failed:

template Foo(T) if (Pred1!T && Pred2!T) {
    ...
}


An alternative syntax (same semantics) similar to a (static) precondition Contract gives more space for error messages:

template Foo(T)
    static in {
        static assert(Pred1!T, "err msg 1");
        static assert(Pred2!T, "err msg 2");
    } body {
        ...
    }

Function templates too may have the optional static precondition, before the optional (run time) precondition.

Bye,
bearophile


More information about the Digitalmars-d mailing list