Getting Field Names of a specific UDA in compile time.

Steven Schveighoffer schveiguy at gmail.com
Sat Oct 3 14:56:32 UTC 2020


On 10/3/20 10:16 AM, realhet wrote:

> I tried it to put into a function:
> 
> auto getSymbolNamesByUDA(T, string uda)(){
>      string[] res;
>      static foreach(a; getSymbolsByUDA!(T, uda)) res ~= a.stringof;
>      return res;
> }

D __traits give you strings, the std.traits thing is giving you symbols. 
Don't use it, just use the compiler traits:

// note, not tested
auto getSymbolNamesByUDA(T, string uda)(){
      string[] res;
      static foreach(n; __traits(allMembers, T)) {
         // static, but don't use static foreach so you can break
	foreach(u; __traits(getAttributes, __traits(getMember, T, n))
            static if(is(typeof(u) == string) && u == uda) {
                res ~= n;
                break;
            }
      }
      return res;
}

-Steve


More information about the Digitalmars-d-learn mailing list