iterate over enum name:value pairs

bearophile bearophileHUGS at lycos.com
Sun Dec 8 11:12:20 PST 2013


Jay Norwood:

> I see comments about enums being somehow implemented as tuples,

Enums are usually implemented as ints, unless you specify a 
different type.


> and comments about tuples somehow being implemented as structs,

Phobos Tuples are implemented with structs.


> but I couldn't find examples of static initialization of arrays 
> of either.

Here it is:


import std.typecons;

enum Foo { A, B, C }
Foo[] a1 = [Foo.A, Foo.B];

alias T = Tuple!(int, int);
T[] a1 = [T(1, 2), T(3, 4)];

void main() {}



> It also doesn't display the  enum bug of hearts2 coming back as 
> hearts in the iteration.

Because those are different strings.


> I tried C static array of struct initialization syntax, and it 
> didn't work, which kind of surprises me.

Here it is:


void main() {
     static struct Suit { string nm; int val; }

     static Suit[5] suits =  [
         {"spades", 1},
         {"hearts", 4}];
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list