October 19, 2015 Enough introspection to report variable name of calling argument | ||||
---|---|---|---|---|
| ||||
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.
|
October 19, 2015 Re: Enough introspection to report variable name of calling argument | ||||
---|---|---|---|---|
| ||||
Posted in reply to Handyman | 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" */
}
----
|
Copyright © 1999-2021 by the D Language Foundation