January 23, 2012
Marco Leise wrote:
>
> 	foreach (i; 0 ... 9, +2)
>

This syntax looks nice. In fact it could replace "foreach_reverse" simply by specifying a negative step value: foreach (i; 9 .. 0, -1)


> also nice would be:
>
> 	foreach (i; 9 ... 0)
>
> The alternative:
>
> 	foreach_reverse(i; 0 .. 10)
>
> is really hard on the human brain :D

Wouldn't this require additional runtime checks? If "foreach (i; 9 .. 0)" was sugar for "foreach (i; 9 .. 0, -1)" in cases where each range value could be determined at compile time that would make sense. Otherwise I'd rather have my brain do a bit more work in these cases over slowing down loops everywhere.


January 23, 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.
January 24, 2012
On Mon, 23 Jan 2012 04:12:23 -0500, Peter Alexander <peter.alexander.au@gmail.com> 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
1 2
Next ›   Last »