www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - foreach with enum

reply Thomas Mader <thezema gmail.com> writes:
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
next sibling parent reply BCS <ao pathlink.com> writes:
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
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
BCS wrote:
 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)
No need. You can iterate over tuples directly. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Jul 19 2007
parent BCS <ao pathlink.com> writes:
Reply to Kirk,

 BCS wrote:
 
 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)
No need. You can iterate over tuples directly.
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);
Jul 19 2007
prev sibling next sibling parent Extrawurst <spam extrawurst.org> writes:
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
prev sibling next sibling parent reply "Vladimir Panteleev" <thecybershadow gmail.com> writes:
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
parent reply BCS <ao pathlink.com> writes:
Reply to Vladimir,

 On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com>
 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.
 
for (Color color=Color.min; color<=Color.max; color++) writefln(color);
enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }
Jul 19 2007
parent reply "Ameer Armaly" <ameer_armaly hotmail.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:ce0a3343bffb8c99827432b23b4 news.digitalmars.com...
 Reply to Vladimir,

 On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com>
 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.
for (Color color=Color.min; color<=Color.max; color++) writefln(color);
enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }
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); }
 
Jul 19 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Ameer Armaly wrote:
 "BCS" <ao pathlink.com> wrote in message 
 news:ce0a3343bffb8c99827432b23b4 news.digitalmars.com...
 Reply to Vladimir,

 On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com>
 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.
for (Color color=Color.min; color<=Color.max; color++) writefln(color);
enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }
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); }
enum Color { Kreuz = 1, None = uint.max } --bb
Jul 19 2007
parent reply "Ameer Armaly" <ameer_armaly hotmail.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:f7p06o$j56$1 digitalmars.com...
 Ameer Armaly wrote:
 "BCS" <ao pathlink.com> wrote in message 
 news:ce0a3343bffb8c99827432b23b4 news.digitalmars.com...
 Reply to Vladimir,

 On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com>
 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.
for (Color color=Color.min; color<=Color.max; color++) writefln(color);
enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }
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); }
enum Color { Kreuz = 1, None = uint.max }
Would do...? Pardon me but I don't see what you're getting at.
 --bb 
Jul 19 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Ameer Armaly wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:f7p06o$j56$1 digitalmars.com...
 Ameer Armaly wrote:
 "BCS" <ao pathlink.com> wrote in message 
 news:ce0a3343bffb8c99827432b23b4 news.digitalmars.com...
 Reply to Vladimir,

 On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com>
 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.
for (Color color=Color.min; color<=Color.max; color++) writefln(color);
enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }
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); }
enum Color { Kreuz = 1, None = uint.max }
Would do...? Pardon me but I don't see what you're getting at.
Enumerating all values from 1 to uint.max could take a long long time. --bb
Jul 19 2007
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Bill Baxter wrote:
 Ameer Armaly wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:f7p06o$j56$1 digitalmars.com...
 Ameer Armaly wrote:
 "BCS" <ao pathlink.com> wrote in message 
 news:ce0a3343bffb8c99827432b23b4 news.digitalmars.com...
 Reply to Vladimir,

 On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema gmail.com>
 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.
for (Color color=Color.min; color<=Color.max; color++) writefln(color);
enum Color { Kreuz = 1, Pik = 2, Herz = 4, Karo = 8 }
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); }
enum Color { Kreuz = 1, None = uint.max }
Would do...? Pardon me but I don't see what you're getting at.
Enumerating all values from 1 to uint.max could take a long long time.
... 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
Jul 19 2007
prev sibling next sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
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
prev sibling parent renoX <renosky free.fr> writes:
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