foreach on interval index by ref increment

Peter Alexander peter.alexander.au at gmail.com
Mon Jan 23 01:12:23 PST 2012


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.


More information about the Digitalmars-d mailing list