digitalmars.D.learn - tell if __traits(allMembers, ... ) is an enum (not manifest constant)
- Nicholas Wilson (9/9) Feb 24 2016 there is no __traits(isEnum, ...)
- Adam D. Ruppe (6/10) Feb 24 2016 That's close but not quite there... try
- Nicholas Wilson (10/20) Feb 24 2016 what is this black magic???
- Nicholas Wilson (5/29) Feb 24 2016 oops should be
- nkgu (11/14) Feb 25 2016 try
- Nicholas Wilson (27/43) Feb 25 2016 Heh. Thanks for the Idea anyway.
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
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
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: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 }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
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:oops should be writeln(typeof(__traits(getMember, vulkan_input, m)).stringof); that compiles but still prints nothingOn Thursday, 25 February 2016 at 04:25:25 UTC, Nicholas Wilson wrote: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 }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
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 nothingtry 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
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: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).oops should be writeln(typeof(__traits(getMember, vulkan_input, m)).stringof); that compiles but still prints nothingtry 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