List all enum in a class

Adam D. Ruppe destructionator at gmail.com
Thu Jun 13 05:29:45 PDT 2013


Try this:
class type {
         enum b = "hey";
         enum test {
                 lol, rofl
         }

         test mem;
}

template workaround(T) {
         alias workaround = T;
}

import std.stdio;
void main() {
         foreach (member; __traits(allMembers, type))
         {
                 static if (is(workaround!(__traits(getMember, 
type, member)) == enum))
                 {
                         writeln("enum type ", member);
                 }
                 else
                 {
                   static if(is(typeof(__traits(getMember, type, 
member)) == enum))
                         writeln("enum member ", member);
                 }
         }

}


It will NOT list b - it considers it to just be a string. But it 
will list test as a type, and mem as an enum typed member.

The reason this will work and yours didn't is typeof(some_type) 
doesn't work because some_type is already a type. An enum is a 
type.

The workaround thing is needed because is(__traits...) doesn't 
compile. It complains that it expected a type, not traits. The 
simple template works around this.

But with the member, it is a value, so you do want to do typeof() 
on that one. So to catch it all, we do both.


However as far as I know right now, there's no way to get "b" as 
an enum here because the typeof will always just return string.


More information about the Digitalmars-d-learn mailing list