On 11/28/18 2:18 AM, Eko Wahyudin wrote:
static foreach(sym; getSymbolsByUDA!(A, Attr)){
    writeln(sym.stringof); // print variable name "a" this what i want
}

foreach(sym; getSymbolsByUDA!(A, Attr)){
    writeln(sym.stringof);              // print "sym"
}

foreach(sym; getSymbolsByUDA!(A, Attr)){
    writeln(__traits(identifier, sym)); //also print "sym"
}

is there a way to get variable name using foreach like static foreach?

Generally, foreach establishes a variable for the item, whereas static foreach does not.

Note: I can't even get this to work, even in old versions of dmd. I get the Error: need `this` for `a` of type `int`

But if you can get it to work, you may have luck with:

alias symbols = getSymbolsByUDA!(A, Attr);

foreach(i, _; symbols)
{
   writeln(symbols[i].stringof);
   // or
   writeln(__traits(identifier, symbols[i]));
}

Which is an old workaround for how foreach on tuples used to work.

-Steve

Reply via email to