digitalmars.D.learn - List all enum in a class
- Bruno Deligny (11/11) Jun 13 2013 Hi,
- Adam D. Ruppe (40/40) Jun 13 2013 Try this:
- bearophile (4/7) Jun 13 2013 Is it worth asking a D enhancement?
- Jonathan M Davis (4/10) Jun 13 2013 It's a manifest constant, so it's not really an enum. As such, I'm incli...
- Adam D. Ruppe (10/11) Jun 13 2013 I don't think so because it isn't something you'd even really
- Bruno Deligny (1/1) Jun 13 2013 i tried your test and it doesn't print test.
- Adam D. Ruppe (6/7) Jun 13 2013 Maybe we have different versions of the compiler (I think this
- Bruno Deligny (3/10) Jun 14 2013 2.062
- Bruno Deligny (2/15) Jun 14 2013 I doesn't work with 2.063
- Adam D. Ruppe (3/4) Jun 16 2013 Hmm, I don't know what's going on. Can someone else try to make
Hi, I tried that but in doesn't print anything: foreach (member; __traits(allMembers, type)) { static if (is(typeof(__traits(getMember, type, member)) == enum)) { writeln(member); } } I tried a lot of other ways but nothing compile. thx
Jun 13 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.
Jun 13 2013
Adam D. Ruppe: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.Is it worth asking a D enhancement? Bye, bearophile
Jun 13 2013
On Thursday, June 13, 2013 14:46:48 bearophile wrote:Adam D. Ruppe:It's a manifest constant, so it's not really an enum. As such, I'm inclined to say no. - Jonathan M DavisHowever 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.Is it worth asking a D enhancement?
Jun 13 2013
On Thursday, 13 June 2013 at 12:46:49 UTC, bearophile wrote:Is it worth asking a D enhancement?I don't think so because it isn't something you'd even really need to know, it is just like writing a literal. Actually that gives me an idea: if you have a variable that is not a function and obviously not a literal since they wouldn't appear on allMembers, checking if you can take the address of it might be a usable check. A regular immutable member and an enum differ in that the member has an address. The enum doesn't. So perhaps there is a way to figure it out, but I still don't think it is worth an enhancement for anyway.
Jun 13 2013
i tried your test and it doesn't print test.
Jun 13 2013
On Thursday, 13 June 2013 at 17:05:43 UTC, Bruno Deligny wrote:i tried your test and it doesn't print test.Maybe we have different versions of the compiler (I think this behavior recently changed). Try running dmd without arguments and see what the first line says. Mine is: DMD32 D Compiler v2.063
Jun 13 2013
On Thursday, 13 June 2013 at 17:13:26 UTC, Adam D. Ruppe wrote:On Thursday, 13 June 2013 at 17:05:43 UTC, Bruno Deligny wrote:2.062 I will try with the 2.063i tried your test and it doesn't print test.Maybe we have different versions of the compiler (I think this behavior recently changed). Try running dmd without arguments and see what the first line says. Mine is: DMD32 D Compiler v2.063
Jun 14 2013
On Friday, 14 June 2013 at 11:44:45 UTC, Bruno Deligny wrote:On Thursday, 13 June 2013 at 17:13:26 UTC, Adam D. Ruppe wrote:I doesn't work with 2.063On Thursday, 13 June 2013 at 17:05:43 UTC, Bruno Deligny wrote:2.062 I will try with the 2.063i tried your test and it doesn't print test.Maybe we have different versions of the compiler (I think this behavior recently changed). Try running dmd without arguments and see what the first line says. Mine is: DMD32 D Compiler v2.063
Jun 14 2013
On Friday, 14 June 2013 at 12:06:11 UTC, Bruno Deligny wrote:I doesn't work with 2.063Hmm, I don't know what's going on. Can someone else try to make it work?
Jun 16 2013