www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - tell if __traits(allMembers, ... ) is an enum (not manifest constant)

reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
there is no __traits(isEnum, ...)

I've tried
foreach(m; __traits(allMembers, ...)
{
     static if (__traits(compiles,EnumMembers!(m)))
     static if (EnumMembers!(m).length)
     static if(is(m== enum))
}

I can detect static functions with __traits(isStaticFunction, ...)
Feb 24 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 25 February 2016 at 04:25:25 UTC, Nicholas Wilson 
wrote:
 foreach(m; __traits(allMembers, ...)
 {
     static if(is(m== enum))
 }
That's close but not quite there... try static if(is(typeof(__traits(getMember, Item, m)) == enum)) for member variables, or if you are looking at a type itself youcan leave off the typeof() bit.
Feb 24 2016
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 25 February 2016 at 04:32:24 UTC, Adam D. Ruppe 
wrote:
 On Thursday, 25 February 2016 at 04:25:25 UTC, Nicholas Wilson 
 wrote:
 foreach(m; __traits(allMembers, ...)
 {
     static if(is(m== enum))
 }
That's close but not quite there... try static if(is(typeof(__traits(getMember, Item, m)) == enum)) for member variables, or if you are looking at a type itself youcan leave off the typeof() bit.
what is this black magic??? static if(is(typeof(__traits(getMember, vulkan_input, m)) == enum)) { writeln(typeof(__traits(getMember, vulkan_input, m))).stringof; //fails to compile writeln(m); //prints nothing }
Feb 24 2016
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 25 February 2016 at 04:50:14 UTC, Nicholas Wilson 
wrote:
 On Thursday, 25 February 2016 at 04:32:24 UTC, Adam D. Ruppe 
 wrote:
 On Thursday, 25 February 2016 at 04:25:25 UTC, Nicholas Wilson 
 wrote:
 foreach(m; __traits(allMembers, ...)
 {
     static if(is(m== enum))
 }
That's close but not quite there... try static if(is(typeof(__traits(getMember, Item, m)) == enum)) for member variables, or if you are looking at a type itself youcan leave off the typeof() bit.
what is this black magic??? static if(is(typeof(__traits(getMember, vulkan_input, m)) == enum)) { writeln(typeof(__traits(getMember, vulkan_input, m))).stringof; //fails to compile writeln(m); //prints nothing }
oops should be writeln(typeof(__traits(getMember, vulkan_input, m)).stringof); that compiles but still prints nothing
Feb 24 2016
parent reply nkgu <nkgu nkgu.gu> writes:
On Thursday, 25 February 2016 at 04:55:09 UTC, Nicholas Wilson 
wrote:
 oops should be
 writeln(typeof(__traits(getMember, vulkan_input,  m)).stringof);
 that compiles but still prints nothing
try pragma(msg, typeof(__traits(getMember, vulkan_input, m)).stringof); because at compile time the code generated for writeln() is not executed. Also take care with the getMember trait, it will only work if the member is public. (or not public but located in the module where the __traits() code resides).
Feb 25 2016
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 25 February 2016 at 08:40:00 UTC, nkgu wrote:
 On Thursday, 25 February 2016 at 04:55:09 UTC, Nicholas Wilson 
 wrote:
 oops should be
 writeln(typeof(__traits(getMember, vulkan_input,  
 m)).stringof);
 that compiles but still prints nothing
try pragma(msg, typeof(__traits(getMember, vulkan_input, m)).stringof); because at compile time the code generated for writeln() is not executed. Also take care with the getMember trait, it will only work if the member is public. (or not public but located in the module where the __traits() code resides).
Heh. Thanks for the Idea anyway. source/app.d(308): Error: module object has no type source/app.d(308): while evaluating pragma(msg, (_error_).stringof) source/app.d(308): Error: type uint is not an expression source/app.d(308): while evaluating pragma(msg, (_error_).stringof) pure nothrow nogc safe int(int major, int minor, int patch) int int pure nothrow nogc safe uint(uint ver) pure nothrow nogc safe uint(uint ver) pure nothrow nogc safe uint(uint ver) ... void void ... source/app.d(308): Error: type VkInstance_T is not an expression source/app.d(308): while evaluating pragma(msg, (_error_).stringof) ... Cheating somewhat static if(m.startsWith("Vk") && __traits(compiles, mixin(m~"."~m.camelToUpper_ ~ "_MAX_ENUM"))) does some of what I want but (unsurprisingly) fails to pick up the bitfield enums (that don't have a "_MAX_ENUM" member).
Feb 25 2016