foreach over string enum

Jesse Phillips jessekphillips+D at gmail.com
Thu Feb 10 20:27:41 PST 2011


spir Wrote:

> But in your example the symbol a does not look like a constant, instead it the 
> loop variable. Do, how does it work? 

Magic.

No really, the best I can tell is that the compiler will try to run the foreach loop at compile-time if there is something in the body that must be evaluated at compile time.

The type you are iterating over must be known at compile-time, and just like any such value it is identified by its type and not its contents. So your array literal could in fact be built with a variable, the fact that it is not doesn't matter.

I'm not sure if much thought has gone into compile-time-looping, the best way to enforce it is to get a function to run at compile time. I think the rule of "when the body needs evaluated at compile-time" is what's used, but this also means that when you try to iterate something like __traits or tupleof and don't use a compile-time construct in the body, you don't get an error or the loop executed.


More information about the Digitalmars-d-learn mailing list