Copy parameters from function passed as an alias

Simen Kjærås simen.kjaras at gmail.com
Thu Oct 11 07:44:04 UTC 2018


On Thursday, 11 October 2018 at 06:30:01 UTC, bauss wrote:
> Because I'm constructing something from the parameters.
>
> To give the real world example.
>
> I'm converting a function into a soap envelope which means the 
> identifiers must be the same because the parameter names passed 
> in the soap envelope must have the same names.

Filed an enhancement request: 
https://issues.dlang.org/show_bug.cgi?id=19299

For now, I believe the giant mixin is your only option, if you 
absolutely need the parameters to have the same names.

If having local variables with the same names is ok, this should 
do it:

import std.stdio;
import std.traits : ParameterIdentifierTuple, Parameters;

auto fun(alias fn)(Parameters!fn args) {
     mixin(copyParameters!fn);
     writeln(a);
}

auto copyParameters(alias fn)() {
     import std.conv : to;
     string result;
     static foreach (i, e; ParameterIdentifierTuple!fn) {
         result ~= "auto "~e~" = args["~i.to!string~"];\n";
     }
     return result;
}

void gun(int a) {}

unittest {
     fun!(gun)(3);
}


Now, is this the only way to inform the soap factory of your 
parameter names? Could you instead pass them more explicitly?

--
   Simen


More information about the Digitalmars-d-learn mailing list