Re: iterate over enum name:value pairs

2013-12-09 Thread bearophile
Jay Norwood: I notice that if Suit and SuitShort have an enum with the same name, then you still have to fully qualify the enum names when using the with statement. So, for example, if spd in SuitShort was renamed spades, the first entry in the array initialization would have to be

Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile
Jay Norwood: enum Suit { spades, hearts=4, diamonds=10, clubs } foreach (i, member; EnumMembers!Suit) Here 'i' is the index of the enumeration type tuple. This code lacks the [] I added in my code, so your foreach is a static one. To tell them apart when I read the code I sometimes

Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood
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

Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile
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

Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood
Yes, thanks, that syntax does work for the initialization. The C syntax that failed for me was using the curly brace form shown in the following link. http://www.c4learn.com/c-programming/c-initializing-array-of-structure/ Also, I think I was trying forms of defining the struct and

Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile
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,

Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile
foreach (immutable member; suits) Sometimes you have to use: foreach (const member; suits) Bye, bearophile

Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood
On Sunday, 8 December 2013 at 22:30:25 UTC, bearophile wrote: Try: member.writeln; Bye, bearophile yeah, that's pretty nice. module main; import std.stdio; void main() { struct Suit {string nm; int val; int val2; string shortNm;}; static Suit[5] suits = [

Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile
static Suit[5] suits = [ {spades,1,6,spd}, {hearts,4,10,hrt}, {hearts2,4,10,hrt2}, {diamonds,10,16,dmd}, {clubs,11,17,clb} Also, in D it's better to put a space after every comma, to increase readability a

Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile
Jay Norwood: struct Suit {string nm; int val; int val2; string shortNm;}; You have missed my suggestions above regarding the struct :-) Look at this: void main() { int x; struct Foo1 { int bar1() { return x; } } pragma(msg, Foo1.sizeof); static struct Foo2 {

Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood
It looks like the writeln() does a pretty good job, even for enum names. I also saw a prettyprint example that prints the structure member name, and compared its output. http://forum.dlang.org/thread/ip23ld$93u$1...@digitalmars.com module main; import std.stdio; import std.traits; void

Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile
Jay Norwood: Using enums, despite their problems, if often better than strings. static Suits[] suits = [ {Suit.spades, 1, 6, SuitShort.spd}, {Suit.hearts, 4, 10, SuitShort.hrt}, {Suit.diamonds, 4, 10, SuitShort.dmd},

Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood
Thanks. That's looking pretty clean. I had already tried the shorter enum names without using the with statement, and it failed to compile. I thought it might work since the struct definition already specifies the enum type for the two members. with (Suit) with (SuitShort) {

Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood
I notice that if Suit and SuitShort have an enum with the same name, then you still have to fully qualify the enum names when using the with statement. So, for example, if spd in SuitShort was renamed spades, the first entry in the array initialization would have to be {Suit.spades, 1, 6,

iterate over enum name:value pairs

2013-12-07 Thread Jay Norwood
In Ali Çehreli very nice book there is this example of iterating over enum range which, as he notes, fails http://ddili.org/ders/d.en/enum.html enum Suit { spades, hearts, diamonds, clubs } foreach (suit; Suit.min .. Suit.max) { writefln(%s: %d, suit, suit); } spades: 0

Re: iterate over enum name:value pairs

2013-12-07 Thread bearophile
Jay Norwood: In Ali Çehreli very nice book there is this example of iterating over enum range which, as he notes, fails http://ddili.org/ders/d.en/enum.html enum Suit { spades, hearts, diamonds, clubs } foreach (suit; Suit.min .. Suit.max) { writefln(%s: %d, suit, suit); }

Re: iterate over enum name:value pairs

2013-12-07 Thread Jay Norwood
Thanks. This is exactly what I was looking for. I tried this iteration below, based on the example shown in the std.traits documentation, and the int values are not what I expected, but your example works fine. http://dlang.org/phobos/std_traits.html#.EnumMembers import std.traits; void