www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Enough introspection to report variable name of calling argument

reply Handyman <handyman handyworld.tv> writes:
Is the following possible in D?  To call a (unary) function f 
with variable a, and let f print:

 Called with argument named 'a'.
I am of course asking for a generic solution, independent of the actual variable name (in this case, 'a'). I am aware that I am likely asking the impossible because of how the function call mechanism is implemented in most programming languages, hence in D. Still, it might be possible given D's stong introspection capabilities / traits / pragma's / whathaveyou's.
Oct 19 2015
parent anonymous <anonymous example.com> writes:
On Monday, October 19, 2015 04:14 PM, Handyman wrote:

 Is the following possible in D?  To call a (unary) function f
 with variable a, and let f print:
 
 Called with argument named 'a'.
I am of course asking for a generic solution, independent of the actual variable name (in this case, 'a'). I am aware that I am likely asking the impossible because of how the function call mechanism is implemented in most programming languages, hence in D. Still, it might be possible given D's stong introspection capabilities / traits / pragma's / whathaveyou's.
I think the exact thing you're asking for is not possible. With slightly different syntax you can do this: ---- module test; void f(alias v)() { import std.stdio: writeln; import std.traits: fullyQualifiedName; writeln(v.stringof, " ", fullyQualifiedName!v); } void main() { int a; f!a(); /* prints "a test.main.a" */ } ----
Oct 19 2015