foreach over string enum

spir denis.spir at gmail.com
Fri Feb 11 01:29:51 PST 2011


On 02/11/2011 05:27 AM, Jesse Phillips wrote:
> 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.

Oh yes, I see. This explains why my example using plain constants (predefined 
values, thus known at compile-time) does not run: there is nothing /forcing/ 
the compiler (such as a ref to a type), thus the compiler does not even try. 
Even if would be less complicated, probably, than with __traits.

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list