www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature Request: foreach over an enum

reply stonecobra <scott stonecobra.com> writes:
Hi,

In helping kris with the mango package (the log administrator servlet 
specifically), I came across the need to iterate over the values of an emum:

enum Level {Trace=0, Info, Warn, Error, Fatal, None};

So, I go about coding like so:

for (int i = Logger.Level.min; i < Logger.Level.max + 1; i++)
{
... //do stuff here with Level[i]
}

Why not add the ability to foreach, so that iteration is nicer:

foreach(Level l; Level)
{
... //do stuff with l
{

Wouldn't you just love to handle enums like this.  I believe iteration 
over enums is a pretty common thing.

Thanks
Scott Sanders
Jun 04 2004
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
stonecobra wrote:

 Hi,

 In helping kris with the mango package (the log administrator servlet 
 specifically), I came across the need to iterate over the values of an 
 emum:

 enum Level {Trace=0, Info, Warn, Error, Fatal, None};

 So, I go about coding like so:

 for (int i = Logger.Level.min; i < Logger.Level.max + 1; i++)
 {
 ... //do stuff here with Level[i]
 }

 Why not add the ability to foreach, so that iteration is nicer:

 foreach(Level l; Level)
 {
 ... //do stuff with l
 {

 Wouldn't you just love to handle enums like this.  I believe iteration 
 over enums is a pretty common thing.

 Thanks
 Scott Sanders

That would be nice. -- -Anderson: http://badmama.com.au/~anderson/
Jun 04 2004
parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
 Wouldn't you just love to handle enums like this.  I believe iteration
 over enums is a pretty common thing.

 Thanks
 Scott Sanders

That would be nice.

I can't immediately see a problem with it. As long as none transpires, count my vote.
Jun 04 2004
prev sibling next sibling parent Sean Kelly <sean f4.ca> writes:
In article <c9r1o4$27nd$1 digitaldaemon.com>, stonecobra says...
Wouldn't you just love to handle enums like this.  I believe iteration 
over enums is a pretty common thing.

I've never had a need to iterate over an enum, but I agree that it's a nice feature. Unless there's some bizarre consequence I haven't thought of it has my vote. Sean
Jun 05 2004
prev sibling parent stonecobra <scott stonecobra.com> writes:
I think that a shortened syntax would reduce the redundancy :)

How about:

foreach(Level l)
{
...
}

That way a single argument would have to be an Enum.

Scott

stonecobra wrote:
 foreach(Level l; Level)
 {
 ... //do stuff with l
 {
 

Jun 06 2004