On Mon, 23 Jan 2012 04:12:23 -0500, Peter Alexander
<[email protected]> wrote:
On Sunday, 22 January 2012 at 03:38:48 UTC, bearophile wrote:
In the last days Walter and other people are closing and fixing many
bugs. But there is one bug that Walter has closed that I am not so sure
about:
http://d.puremagic.com/issues/show_bug.cgi?id=5306
I completely agree with your analysis.
foreach (i; 0..10) means to do something for every integer in the 0..10
range. It does *not* mean "start an integer at 0 and repeatedly do
something then increment it until it reaches 10". That's the
implementation detail. Adding ref should not leak the implementation.
It doesn't for foreach (ref i; iota(0, 10))
It doesn't for foreach (ref i; /* an array of 0..10 */)
Why should foreach (ref i; 0..10) be a special case?
Arguing that it is sometimes convenient is not a strong argument. There
are plenty of things that are sometimes convenient (e.g. implicit
casting between any type), but are error-prone and disallowed for good
reasons.
If you want control over the way the index variable increments then use
a standard for-loop. That's what it's there for.
I think the ref version is not an issue. I personally think it should be
invalid syntax, like this is invalid syntax:
foreach(ref i, x; [1,2,3,4,5])
But if it has to be valid, then the current behavior makes sense.
However, my biggest issue is with:
foreach(i; 1..10)
++i; // alters iteration.
IOW, see Martin's bug. That is a real issue.
-Steve