How do I iterate over enum members at runtime?
Jesse Phillips
jessekphillips+D at gmail.com
Sat Apr 9 18:42:01 PDT 2011
You must iterate a compile time only construct. So yes it would be nice to be able to say: static foreach and have the compiler tell you, no I can't do that (and force it if it can).
Andrej Mitrovic Wrote:
> Well in this case it works, but it's not always so easy. For example:
>
> import std.conv;
> enum Metrics : int
> {
> val0,
> val1,
> val2
> }
>
> void foo(int m)
> {
> }
>
> void main()
> {
> foreach (index; 0..3)
> {
> foo(mixin("Metrics.val" ~ to!string(index)));
> }
> }
>
> So even though you might think "hey, it's obvious index in this case
> can never be anything other than 0, 1, or 2", you still won't be able
> to compile this. CTFE is a tricky thing.
More information about the Digitalmars-d-learn
mailing list