iterate over enum name:value pairs

Jay Norwood jayn at prismnet.com
Sun Dec 8 10:56:16 PST 2013


I see comments about enums being somehow implemented as tuples, 
and comments about tuples somehow being implemented as structs, 
but I couldn't find examples of static initialization of arrays 
of either.

Finally after playing around with it for a while, it appears this 
example below works for static array of struct initialization.  
It also doesn't display the  enum bug of hearts2 coming back as 
hearts in the iteration.

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

module main;

import std.stdio;

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

	static Suit[5] suits =  [
			Suit("spades",1),
			Suit("hearts",4),
			Suit("hearts2",4),
			Suit("diamonds",10),
			Suit("clubs",11)
		];


     foreach (member;  suits)
     {
		writefln("%s: %d", member.nm, member.val);
     }

}

D:\dprojects\ConsoleApp1\ConsoleApp1>dmd -run main.d
spades: 1
hearts: 4
hearts2: 4
diamonds: 10
clubs: 11


More information about the Digitalmars-d-learn mailing list