Thread overview
Feature Request: foreach over an enum
Jun 04, 2004
stonecobra
Jun 05, 2004
J Anderson
Jun 05, 2004
Matthew
Jun 05, 2004
Sean Kelly
Jun 07, 2004
stonecobra
June 04, 2004
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
June 05, 2004
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/
June 05, 2004
> > 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.



June 05, 2004
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


June 07, 2004
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
> {
>