metaprogramming question

Philippe Sigaud philippe.sigaud at gmail.com
Sun Apr 18 22:28:09 PDT 2010


On Mon, Apr 19, 2010 at 05:21, Justin Spahr-Summers <
Justin.SpahrSummers at gmail.com> wrote:

> You can use some expression tuple magic to accomplish something like
> that:
>
> bool check(alias func, EL ...)() {
>    GError* err;
>    bool ok = func(EL, &err);
>     if (!ok)
>        throw new Exception((*err).message);
>
>     return ok;
> }
>
> // used like:
> check!(fooXXX, arg1, ..., argN);
>
>
But in this case, you need to know the ELs at compile-time. You can make
them run-time values that way:

bool check(alias func, EL ...)(EL el) {
   GError* err;
   bool ok = func(el, &err);
    if (!ok)
       throw new Exception((*err).message);

    return ok;
}

// used like:
check!fooXXX(arg1, ..., argN);


Of course, it'd be nice to check the EL at CT to see if they correspond to
func parameters types. I don't know if you can do this with C functions, but
for D funcs you can add:

import std.traits;
bool check(alias func, EL ...)(EL el) if (is(ParameterTypeTuple!func[0..$-1]
== TypeTuple!(EL, GError*))
{
...

It's a bit strict, as it doesn't deal with implicit conversions.

Cheers,

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


More information about the Digitalmars-d-learn mailing list