foreach with enum

BCS ao at pathlink.com
Thu Jul 19 14:31:31 PDT 2007


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);





More information about the Digitalmars-d mailing list