iterate over enum name:value pairs

bearophile bearophileHUGS at lycos.com
Sun Dec 8 14:30:24 PST 2013


Jay Norwood:

> If that is so, then the C initialization of an array with an 
> unnamed struct type, like this, would require a struct type 
> name.
>
> static struct { int i; long lv;} suits[3] = {{1, 2L},{2, 
> 4L},{3,9L}};

Giving a struct a name is often a good idea. But if you don't 
want to name it, then you can use a Phobos Tuple. You can even 
omit its field names if you want.


> 	struct Suit {string nm; int val; int val2; string shortNm;};

Better:

         static struct Suit {string nm; int val; int val2; string 
shortNm;}

Generally inside functions it's better to define static struct. 
And struct definitions don't need a trailing semicolon.


>     foreach (member;  suits)

Generally it's better to attach an 'immutable' there, this is not 
always possible, but it's a good habit:

       foreach (immutable member;  suits)


> I also tried using writefln(tup) and writeln(tup) in the 
> example above.  The output from writeln(tup) looks like it is 
> headed in the right direction.  Maybe a writecsv(tup) would be 
> useful.

Try:

member.writeln;

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list