www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get function argument name?

reply JN <666total wp.pl> writes:
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?
Mar 04 2018
parent reply arturg <var.spool.mail700 gmail.com> writes:
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); }
Mar 04 2018
parent reply JN <666total wp.pl> writes:
On Sunday, 4 March 2018 at 21:12:44 UTC, arturg wrote:
 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);
 }
Well, it works. But I am confused now, why. Isn't alias only for types (like a typedef)? Why can we use it for variable here?
Mar 04 2018
parent reply bauss <jj_1337 live.dk> writes:
On Sunday, 4 March 2018 at 21:48:53 UTC, JN wrote:
 On Sunday, 4 March 2018 at 21:12:44 UTC, arturg wrote:
 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);
 }
Well, it works. But I am confused now, why. Isn't alias only for types (like a typedef)? Why can we use it for variable here?
Because it is what it is, an alias. Not a type. It can be a type, expression or member/variable.
Mar 04 2018
parent Timothee Cour <thelastmammoth gmail.com> writes:
`printName(alias var)()` is not a great solution, eg: doesn't work
with expressions, doesn't work with variadics, introduces template
bloat.

https://github.com/dlang/dmd/pull/7821 introduces
__traits(getCallerSource, symbol) which will allow what you want.

On Sun, Mar 4, 2018 at 1:53 PM, bauss via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
 On Sunday, 4 March 2018 at 21:48:53 UTC, JN wrote:
 On Sunday, 4 March 2018 at 21:12:44 UTC, arturg wrote:
 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);
 }
Well, it works. But I am confused now, why. Isn't alias only for types (like a typedef)? Why can we use it for variable here?
Because it is what it is, an alias. Not a type. It can be a type, expression or member/variable.
Mar 04 2018