digitalmars.D - foreach with enum
- Thomas Mader (5/5) Jul 19 2007 I am just porting some of my little Java programs to D to get comfortabl...
- BCS (5/17) Jul 19 2007 I think you are out of luck. Really enums should have a .tupleof proprie...
- Kirk McDonald (7/30) Jul 19 2007 No need. You can iterate over tuples directly.
- BCS (11/36) Jul 19 2007 however if the code can use a non tuple iteration, you wold be better of...
- Extrawurst (2/8) Jul 19 2007
- Vladimir Panteleev (9/14) Jul 19 2007 ble with the langugage and realized that I cannot use foreach with Enums...
- BCS (2/19) Jul 19 2007 enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }
- Ameer Armaly (9/29) Jul 19 2007 Maybe make enums work with the in operator? I.E:
- Bill Baxter (3/35) Jul 19 2007 enum Color { Kreuz = 1, None = uint.max }
- Ameer Armaly (3/38) Jul 19 2007 Would do...? Pardon me but I don't see what you're getting at.
- Bill Baxter (3/41) Jul 19 2007 Enumerating all values from 1 to uint.max could take a long long time.
- Bill Baxter (6/48) Jul 19 2007 ... though I should add that I see nothing wrong with the idea of making...
- Robert Fraser (5/11) Jul 19 2007 Hi Thomas,
- renoX (10/16) Jul 23 2007 enum are woefully underpowered in D, you cannot print them correctly,
I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.
Jul 19 2007
Reply to Thomas,I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.I think you are out of luck. Really enums should have a .tupleof propriety. Then this would work: foreach(A a; [A.tupleof]){...} (if putting a tuple inside an array worked as expected)
Jul 19 2007
BCS wrote:Reply to Thomas,No need. You can iterate over tuples directly. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.orgI am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.I think you are out of luck. Really enums should have a .tupleof propriety. Then this would work: foreach(A a; [A.tupleof]){...} (if putting a tuple inside an array worked as expected)
Jul 19 2007
Reply to Kirk,BCS wrote:however if the code can use a non tuple iteration, you wold be better of with the array version because it only generates the code once. foreach(i;T!(1,2,3) { writef(i); } goes to writef(1); writef(2); writef(3);Reply to Thomas,No need. You can iterate over tuples directly.I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.I think you are out of luck. Really enums should have a .tupleof propriety. Then this would work: foreach(A a; [A.tupleof]){...} (if putting a tuple inside an array worked as expected)
Jul 19 2007
that would be a very nice feature indeed Thomas Mader schrieb:I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.
Jul 19 2007
On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com> wro= te:I am just porting some of my little Java programs to D to get comforta=ble with the langugage and realized that I cannot use foreach with Enums= .In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.for (Color color=3DColor.min; color<=3DColor.max; color++) writefln(color); -- = Best regards, Vladimir mailto:thecybershadow gmail.com
Jul 19 2007
Reply to Vladimir,On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com> wrote:enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.for (Color color=Color.min; color<=Color.max; color++) writefln(color);
Jul 19 2007
"BCS" <ao pathlink.com> wrote in message news:ce0a3343bffb8c99827432b23b4 news.digitalmars.com...Reply to Vladimir,Maybe make enums work with the in operator? I.E: enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 } for(int i = Color.min; i <= Color.max; ++i) { if( i in Color) writefln(i); }On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com> wrote:enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.for (Color color=Color.min; color<=Color.max; color++) writefln(color);
Jul 19 2007
Ameer Armaly wrote:"BCS" <ao pathlink.com> wrote in message news:ce0a3343bffb8c99827432b23b4 news.digitalmars.com...enum Color { Kreuz = 1, None = uint.max } --bbReply to Vladimir,Maybe make enums work with the in operator? I.E: enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 } for(int i = Color.min; i <= Color.max; ++i) { if( i in Color) writefln(i); }On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com> wrote:enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.for (Color color=Color.min; color<=Color.max; color++) writefln(color);
Jul 19 2007
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:f7p06o$j56$1 digitalmars.com...Ameer Armaly wrote:Would do...? Pardon me but I don't see what you're getting at."BCS" <ao pathlink.com> wrote in message news:ce0a3343bffb8c99827432b23b4 news.digitalmars.com...enum Color { Kreuz = 1, None = uint.max }Reply to Vladimir,Maybe make enums work with the in operator? I.E: enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 } for(int i = Color.min; i <= Color.max; ++i) { if( i in Color) writefln(i); }On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com> wrote:enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.for (Color color=Color.min; color<=Color.max; color++) writefln(color);--bb
Jul 19 2007
Ameer Armaly wrote:"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:f7p06o$j56$1 digitalmars.com...Enumerating all values from 1 to uint.max could take a long long time. --bbAmeer Armaly wrote:Would do...? Pardon me but I don't see what you're getting at."BCS" <ao pathlink.com> wrote in message news:ce0a3343bffb8c99827432b23b4 news.digitalmars.com...enum Color { Kreuz = 1, None = uint.max }Reply to Vladimir,Maybe make enums work with the in operator? I.E: enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 } for(int i = Color.min; i <= Color.max; ++i) { if( i in Color) writefln(i); }On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com> wrote:enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.for (Color color=Color.min; color<=Color.max; color++) writefln(color);
Jul 19 2007
Bill Baxter wrote:Ameer Armaly wrote:... though I should add that I see nothing wrong with the idea of making 'in' work for enums itself. That would be nice. But it's not a replacement for a way to iterate over all values in an enum, which is what the thread is about. --bb"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:f7p06o$j56$1 digitalmars.com...Enumerating all values from 1 to uint.max could take a long long time.Ameer Armaly wrote:Would do...? Pardon me but I don't see what you're getting at."BCS" <ao pathlink.com> wrote in message news:ce0a3343bffb8c99827432b23b4 news.digitalmars.com...enum Color { Kreuz = 1, None = uint.max }Reply to Vladimir,Maybe make enums work with the in operator? I.E: enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 } for(int i = Color.min; i <= Color.max; ++i) { if( i in Color) writefln(i); }On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com> wrote:enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.for (Color color=Color.min; color<=Color.max; color++) writefln(color);
Jul 19 2007
Thomas Mader Wrote:I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.Hi Thomas, D enums are effectively just collections of integral constants, optionally with a namespace. Java enums are a lot more powerful (though I think they go a little bit overboard -- if you're adding methods to your enum members, isn't it about time you made them a true class hierarchy?). I created a mixinable template generating an enum-like struct that overcomes many of the shortcomings of D's enum (there's a "toString" method to get the name, a "members" static method to get a collection of all the members (so you can loop through them just fine), and it's possible to check if a particular member exists by using an is expression). I've also created a "BitSwitch" template that's a bit harder to work with, but with the same advantages. They're just structs with a single uint member, so they're no less efficient than enums are now. I can post both of them if you want, but others have probably come up with (better) implementations (I think someone on learn mentioned they hand done something similar).
Jul 19 2007
Thomas Mader a écrit :I am just porting some of my little Java programs to D to get comfortable with the langugage and realized that I cannot use foreach with Enums. In Java I can do: enum Color { Kreuz, Pik, Herz, Karo } for(Color color : Color.values()) {} AFAIK thats not possible in D yet.enum are woefully underpowered in D, you cannot print them correctly, cannot iterate over them,.. It's a pity because I think that it's a very important structure (well less than array literals, but still important). I was inspired by Robert to make also a templated enum version to print them, (and I'm probably not the only one) but it's a hack.. With macros, hopefully these hacks could become 'less ugly' enough that we could integrate them in a library.. Regards, renoX
Jul 23 2007