getting Key:Value pairs of an enum at compile time ?

Stanislav Blinov stanislav.blinov at gmail.com
Thu Jan 23 10:07:31 PST 2014


On Thursday, 23 January 2014 at 17:59:05 UTC, Uplink_Coder wrote:

First and foremost, I'd suggest you use the beautiful solution 
proposed by Meta.

But to illustrate problems with your code:

>   foreach (i,Item;[__traits(allMembers,Enum)]) {

This is the culprit. More specifically, you don't need to put the 
result of __traits into array. __traits(allMembers) returns a 
tuple which can be iterated at compile time. This is how it would 
look:

auto EnumToSelect(Enum)(string Name = __traits(identifier,Enum)) 
if (is(Enum==enum)) {
	string form_string;
         // Note, there are no [] enclosing __traits
	foreach (i,Item;__traits(allMembers,Enum)) {
		form_string ~=
			"\toption(value='"~Item~"') "
			~__traits(getMember,Enum,Item)~"\n";
	}
	return form_string;
}


More information about the Digitalmars-d-learn mailing list