suggested change to foreach index

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Jun 8 13:49:11 PDT 2006


"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. 





More information about the Digitalmars-d mailing list