suggested change to foreach index

BCS BCS_member at pathlink.com
Sat Jun 10 11:28:50 PDT 2006


In article <e6ev28$o1k$1 at digitaldaemon.com>, Deewiant says...
>
>> 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.
>>>
>
>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.

Yup, a type inference keyword would be nice.

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

Come to think of it, that would be consistent, the variable never caries in a
value at the start of the loop. Also it sounds nice: " this variable is outside
this scope"


On a different track:

given this

<code>
int i;
foreach(out i, char c; "ab"){i=3}
writef(i,\n);
</code>

what is printed? 1, 2 or 3?

It is 1 if i exits with the last value the loop sets it to.
It is 2 if i exits with the first value that doesn't match
It is 3 if i keeps its value at the point the loop is exited.

I would go with 1 or maybe 3. 2 becomes undefined with an AA

Thoughts? Comments?





More information about the Digitalmars-d mailing list