foreach with enum

Bill Baxter dnewsgroup at billbaxter.com
Thu Jul 19 19:24:29 PDT 2007


Bill Baxter wrote:
> Ameer Armaly wrote:
>> "Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message 
>> news:f7p06o$j56$1 at digitalmars.com...
>>> Ameer Armaly wrote:
>>>> "BCS" <ao at pathlink.com> wrote in message 
>>>> news:ce0a3343bffb8c99827432b23b4 at news.digitalmars.com...
>>>>> Reply to Vladimir,
>>>>>
>>>>>> On Thu, 19 Jul 2007 23:14:59 +0300, Thomas Mader <thezema at 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



More information about the Digitalmars-d mailing list