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

Rikki Cattermole alphaglosined at gmail.com
Thu Jan 23 07:23:06 PST 2014


On Thursday, 23 January 2014 at 15:07:18 UTC, Uplink_Coder wrote:
> Hello,
>
> I have an Enum to represent possible Options for a selectList
> e.g
> enum options {
>   blueish="Blue",
>   redish ="Red"
> }
> and I want the List-Entrys to say
> "blueish" or "redish".
> the value send to the app should be
> "Blue" or "Red".
> I can use __traits(allMembers,Enum)
> for the identifiers, but how do I get the Values ?

import std.stdio;

enum Options {
   blueish="Blue",
   redish ="Red"
}

void main() {
	foreach(m; __traits(allMembers, Options)) {
		writeln(m);	
		writeln(cast(string)__traits(getMember, Options, m));	
	}
}

The cast forces it to grab the value. Note where Options values 
are of type string. Its the same as explicitly saying enum T : 
string {.


More information about the Digitalmars-d-learn mailing list