A const idiom + a different 'delete'

bearophile bearophileHUGS at lycos.com
Thu Aug 12 04:54:26 PDT 2010


Shin Fujishiro:
> import std.conv;
> 
> auto ref reverseArgs(alias fun, Args...)(auto ref Args args)
> {
>     return mixin(
>         {
>             string rargs;
>             foreach (i, Arg; Args)
>             {
>                 if (i > 0)
>                     rargs ~= ", ";
>                 rargs ~= "args[" ~ to!string(args.length - i - 1) ~ "]";
>             }
>             return "fun(" ~ rargs ~ ")";
>         }());
> }
> 
> void main()
> {
>     int div(int a, int b) { return a / b; }
> 
>     assert(reverseArgs!div(3, 27) == 9);
> }

I think this is more readable (not tested much):

import std.typecons: TypeTuple;

// this needs to be template of std.typecons
template ReverseTypeTuple(T...) { // too much long name
    static if (T.length)
        alias TypeTuple!(ReverseTypeTuple!(T[1 .. $]), T[0]) ReverseTypeTuple;
    else
        alias T ReverseTypeTuple;
}

auto ref reverseArgs(alias fun, Args...)(auto ref Args args) {
    return fun(ReverseTypeTuple!args);
}

int div(int a, int b) { return a / b; }

void main() {
    assert(reverseArgs!div(3, 27) == 9);
}

Bye,
bearophile


More information about the Digitalmars-d mailing list