suggested change to foreach index

Sean Kelly sean at f4.ca
Thu Jun 8 15:55:57 PDT 2006


Jarrett Billingsley wrote:
> "BCS" <BCS at pathlink.com> wrote in message 
> news:e69lah$dmo$1 at digitaldaemon.com...
> 
>> The suggested syntax
>>            vvv
>> foreach([[inout] index,] value ; ...
>>
>> seems a bit odd to me. How about use "alias" instead?
>>
>> int i;
>> foreach(alias i, v; arr){}
> 
> Or, more consistent, make auto foreach indices require "auto"?
> 
> int i;
> 
> foreach(auto i, auto j; something)
>     writefln(i); // New local i which overrides outer i
> 
> foreach(i, auto j; something)
>     writefln(i); // Uses outer i
> 
> writefln(i); // Writes the value that i last had in the loop
> 
> This has the (wonderful) side-effect of making it bleedingly obvious that 
> the foreach indices are using type inference; the current index inference 
> form _looks_ like it's trying to use already-defined index variables. 

I was thinking the same thing.  Definitely has my vote.


Sean



More information about the Digitalmars-d mailing list