Thread overview | |||||
---|---|---|---|---|---|
|
April 01, 2008 enum and foreach | ||||
---|---|---|---|---|
| ||||
if there is any way to use named enum as source of foreach? For example like enum X {A, B, C} foreach(x; X) ... I have enum that can be extened in future. But I dont want to refactor sources every time I I extend enum |
April 01, 2008 Re: enum and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to yvad | yvad <yannurov@gmail.com> wrote: > if there is any way to use named enum as source of foreach? For example like > > enum X {A, B, C} > > foreach(x; X) ... > > I have enum that can be extened in future. But I dont want to refactor sources every time I I extend enum You can do: for(auto x = X.min; x <= X.max; x++) { ... } But note however that this will execute your body once for each numeric value between the lowest and the highest in your enum, not for each defined member. ie if you do: enum X {A=50, B=50, C=100}; The loop will be run for 50, 51, 52, ..., 99, 100. Henning -- GPG Public Key: http://gpg-keyserver.de/pks/lookup?op=get&search=0xDDD6D36D41911851 |
April 01, 2008 Re: enum and foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Henning Hasemann | Henning Hasemann Wrote:
> yvad <yannurov@gmail.com> wrote:
> > if there is any way to use named enum as source of foreach? For example like
> >
> > enum X {A, B, C}
> >
> > foreach(x; X) ...
> >
> > I have enum that can be extened in future. But I dont want to refactor sources every time I I extend enum
>
> You can do:
> for(auto x = X.min; x <= X.max; x++) { ... }
>
> But note however that this will execute your body once for each numeric value between the lowest and the highest in your enum, not for each defined member. ie if you do:
>
> enum X {A=50, B=50, C=100};
>
> The loop will be run for 50, 51, 52, ..., 99, 100.
>
> Henning
>
> --
> GPG Public Key: http://gpg-keyserver.de/pks/lookup?op=get&search=0xDDD6D36D41911851
>
This the main reason for foreach.
|
Copyright © 1999-2021 by the D Language Foundation