Enough introspection to report variable name of calling argument
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Oct 19 07:55:07 PDT 2015
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" */
}
----
More information about the Digitalmars-d-learn
mailing list