using __traits to get line number of a member

Ali Çehreli acehreli at
Sat Nov 13 07:20:14 UTC 2021

On 11/12/21 11:00 PM, forkit wrote:

 > // nope. writes the line no. of the foreach loop

mixin(m) seems to solve the issue, which I think necessitates 'static if':

     static foreach(m; __traits(allMembers, mixin(__MODULE__)))
         static if(m == "std" || m == "object" || m == "main")
           // Ignore
         } else {
           writefln("The name of the first user member is: %s", m);
           writeln(__traits(getLocation, mixin(m))[1]);  // <-- HERE

It works because we mix-in the value of the string 'm', which becomes a 

('foreach' instead of 'static foreach' works as well.)


More information about the Digitalmars-d-learn mailing list