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

Meta jared771 at gmail.com
Thu Jan 23 09:29:54 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.traits;
import std.range;
import std.conv;

enum Nums
{
     one,
     two,
     three,
     four,
     five,
}
void main()
{
     //For a range of tuples
     auto kvRange = zip([EnumMembers!Nums],
                        [EnumMembers!Nums]
                            .to!(OriginalType!Nums[]))

     //For an associative array
     import std.array;
     auto aa = kvRange.assocArray();
}


More information about the Digitalmars-d-learn mailing list