How can I get the variable name passed as parameter from within a function?

Jack jckj33 at gmail.com
Fri Feb 26 19:32:52 UTC 2021


int a = 10;
f(a); // print "a"
int b = 10;
f(b); // print "b"

I managed to do this with alias parameter in a template:

template f(alias s, string file = __FILE__, size_t line = 
__LINE__)
{
     import std.exception : enforce;
     import std.string : format;

     void g()
     {
       writeln(__traits(identifier, s));
     }
}

so I can call like this:

f!(a).g; // print "a"

Are there other way to do this? Also, can I short this template 
function somehow to syntax f!(a) omitting the g? I couldn't find 
a way to set the template's body like it's a function


More information about the Digitalmars-d-learn mailing list