On Sunday, 9 March 2014 at 23:14:06 UTC, bearophile wrote:
How can it be an implementation detail if the compiler accepts
code like this?
That's really just a CTFE limitation, not an entirely different
kind of loop. If ctfe carried across just a little bit more, this
could work too:
foreach(immutable a; [1,2,3])
mixin(a);
The major, fundamental difference between static foreach as
proposed and foreach is:
* static foreach does not introduce a new scope. If you take your
code and try to access x0 outside the loop, it will fail. With a
static foreach, that would be allowed (just like how static if
can declare variables)
* static foreach would be valid at module level and inside
templates (like static if), whereas regular foreach is only
permitted in a function (like regular if).
You can't do anything like this with a dynamic foreach, it must
to be unrolled at compile-time:
Note that the optimizer is allowed to unroll any loop it desires.
You can have local variables in a regular runtime loop too, but
you can't refer to them outside, so the unrolling here is a code
generator implementation detail rather than a fundamental
difference in the language.