Obtaining argument names in (variadic) functions

jkpl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 16 13:43:09 PDT 2016


On Wednesday, 16 March 2016 at 20:24:38 UTC, data pulverizer 
wrote:
> Hi D gurus,
>
> is there a way to obtain parameter names within the function 
> body? I am particularly interested in variadic functions. 
> Something like:
>
> void myfun(T...)(T x){
>     foreach(i, arg; x)
>         writeln(i, " : ", arg);
> }
>
> void main(){
>     myfun(a = 2, b = "two", c = 2.0);
> }
>
> // should print
> a : 2
> b : two
> c : 2.0
>
> Thanks in advance
>
> Loving the mixins and tuples

I try to anticipate the reason why you want this. As said in a 
previous answer you can access to an individual element by using 
the array syntax but also _param_<X>, with X the index of the 
parameter:

void myfun(T...)(T x)
{
     import std.traits; import std.stdio;
     writeln(ParameterIdentifierTuple!(myfun!T));
     writeln(_param_0);
     writeln(_param_1);
     writeln(_param_2);
}

void main()
{
     int a=1,b=2,c=3;
     myfun(a,b,c);
}


More information about the Digitalmars-d-learn mailing list