suggested change to foreach index

Deewiant deewiant.doesnotlike.spam at gmail.com
Sat Jun 10 10:20:34 PDT 2006


Bruno Medeiros wrote:
> Jarrett Billingsley wrote:
>> 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.
>>
> 
> Agreed too, this is the natural/intuitive/consistent way to go.
> Those other solutions with "alias" and "inout" were awful.
> 

I guess I'm the only one that doesn't like this idea, then. I think "auto" is
overloaded enough as it is; I'm still waiting for "var" or some such for type
inference.

And I always thought it was obvious that foreach() uses its own index, but that
might be since I've only recently (say, a few days back) begun to use type
inference with it. When you write "size_t i, type x; something" it's obvious
enough, and when I write "i, type x" I think of the longer form, realising it's
short for just that.

Personally, I'd prefer "out" instead of "inout" or "alias", but that's just me. <g>



More information about the Digitalmars-d mailing list