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

Reply via email to