metaprogramming question

Philippe Sigaud philippe.sigaud at gmail.com
Sun Apr 18 22:48:24 PDT 2010


On Mon, Apr 19, 2010 at 07:31, BCS <none at anon.com> wrote:

> Or you can mangel that a bit:
>
> template check(alias func) {
>  bool check(EL ...)() {
>
>     GError* err;
>     bool ok = func(EL, &err);
>     if (!ok)
>        throw new Exception((*err).message);
>     return ok;
>  }
> }
>
> that should allow you to call it like:
>
> check!(fooXXX)(arg1, ..., argN);


(I guess you meant bool check(EL...)(EL el) in the second stage.)

Yes, this example is even more powerful. The version I posted already allows
you to write

check!fun(params); // no need to give explictly the EL, the compiler wil
deduce them for you

 but yours allow you to 'store' the checking of fun and then to use it for
any parameter typetuple. Maybe not useful for the OP, but mighty handy in
some other cases.
I discovered this trick a few weeks ago and, though a bitch to code
sometimes, it's very nice to have.

Usage:

alias check!foo fooCheck; // checkFoo is a templated function, so it's _not_
a function. You cannot assign it to a variable.
                                                 // use alias instead.

// some other code //
auto check1 = fooCheck(1,2,3);
//
auto check2 = fooCheck("a","b", 2.34);

It's particularly useful when foo is polymorphic. Again, not interesting for
the OP, but very fun.

Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100419/41ba3a9d/attachment.html>


More information about the Digitalmars-d-learn mailing list