Get function argument name?

arturg var.spool.mail700 at gmail.com
Sun Mar 4 21:12:44 UTC 2018


On Sunday, 4 March 2018 at 21:03:04 UTC, JN wrote:
> Imagine a function like this:
>
> void printValue(T)(string name, T value)
> {
>   writeln(name, " = ", value);
> }
>
> int x = 10;
> printValue("x", x);
>
> is it somehow possible to convert that printValue into a mixin 
> or something, so I could do something like:
>
> printValue(x);
>
> and it will figure out the "x" part automatically?

you can pass it by alias:

import std.stdio;

void main(string[] args)
{
     int x;
     printName!(x);
}

void printName(alias var)()
{
     writeln(__traits(identifier, var), " ", var);
}



More information about the Digitalmars-d-learn mailing list