www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - List all enum in a class

reply "Bruno Deligny" <bruno.deligny gmail.com> writes:
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
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, June 13, 2013 14:46:48 bearophile wrote:
 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?
It's a manifest constant, so it's not really an enum. As such, I'm inclined to say no. - Jonathan M Davis
Jun 13 2013
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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
prev sibling parent reply "Bruno Deligny" <bruno.deligny gmail.com> writes:
i tried your test and it doesn't print test.
Jun 13 2013
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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
parent reply "Bruno Deligny" <bruno.deligny gmail.com> writes:
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:
 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
2.062 I will try with the 2.063
Jun 14 2013
parent reply "Bruno Deligny" <bruno.deligny gmail.com> writes:
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:
 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
2.062 I will try with the 2.063
I doesn't work with 2.063
Jun 14 2013
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 14 June 2013 at 12:06:11 UTC, Bruno Deligny wrote:
 I doesn't work with 2.063
Hmm, I don't know what's going on. Can someone else try to make it work?
Jun 16 2013