Andrej Mitrovic:

Actually it used to be a bug that writing to the index /without/ ref
would end up changing the iteration order, but this was fixed in
2.063. It's in the changelog:

http://dlang.org/changelog.html#foreachref

The right design in my opinion is to have the iteration variable immutable on default, and mutable/reference on request. This saves from bugs and offers new optimization opportunities. But unfortunately D doesn't have a "mutable" keyword, D variables are generally mutable on default, and Walter seemed not interested in my numerous explanations that the mutable foreach iteration variable is bug-prone. So Hara has adopted a compromise, now if you don't use "ref" the actual iteration variable on an interval doesn't change. But it's mutable on default.

So the standard idiom to use foreach on interval needs to be:

foreach (immutable i; 0 .. 10) { ... }

And the programmer has to remove that immutable only where really the iteration variable must change :-)

Bye,
bearophile

Reply via email to