Obtaining argument names in (variadic) functions
data pulverizer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Mar 16 13:57:55 PDT 2016
On Wednesday, 16 March 2016 at 20:53:42 UTC, JR wrote:
> 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
>
> You can do it precisely like that if the variables/symbols you
> pass as (template) arguments are properly declared first.
>
> http://dpaste.dzfl.pl/0b452efeaaab
>
>
> void printVars(Args...)()
> if (Args.length > 0)
> {
> import std.stdio : writefln;
>
> foreach (i, arg; Args) {
> writefln("%s\t%s:\t%s", typeof(Args[i]).stringof,
> Args[i].stringof, arg);
> }
> }
>
> void main() {
> int abc = 3;
> string def = "58";
> float ghi = 3.14f;
> double jkl = 3.14;
>
> printVars!(abc,def,ghi,jkl)();
> }
That's brilliant! Thanks JR
More information about the Digitalmars-d-learn
mailing list