using __traits to get line number of a member

Ali Çehreli acehreli at yahoo.com
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 
symbol.

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

Ali



More information about the Digitalmars-d-learn mailing list