About foreach loops

Ali Çehreli acehreli at yahoo.com
Tue Jun 14 23:56:10 PDT 2011


On Tue, 14 Jun 2011 23:45:11 -0500, Caligo wrote:

> This might make it clear:
> 
>   foreach(i, e; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]){
>     i += 1;
>     writeln(i, " ", e);
>   }
> 
> which outputs:
> 1 0
> 3 2
> 5 4
> 7 6
> 9 8

I think that example strengthens bearophile's point of view. I would 
expect 'e' to provide access to all of the elements regardless of whether 
we asked for the extra index information or not.

The fact that modifying the extra information changes the elements that 
get visited is confusing.

Despite 'e' being a copy of the element, 'i's being a reference to the 
actual implementation variable is extra confusing. :)

Ali


More information about the Digitalmars-d mailing list