June 08, 2006 Re: suggested change to foreach index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | Sean Kelly wrote:
> Deewiant wrote:
>> Sean Kelly wrote:
>>> void main()
>>> {
>>> int i;
>>> char[] arr = "abc def";
>>>
>>> foreach( i, c; arr )
>>> {
>>> printf( "%i\n", i );
>>> if(' ' == c)
>>> break;
>>> }
>>> printf( "\n%i\n", i );
>>> }
>>
>> It creates a new scope, just like for loops:
>>
>> int i = 5;
>> for (int i = 0; i < 3; ++i)
>> writefln(i); // 0 to 2
>> writefln(i); // 5
>
> Yes, but my example was equivalent to this (I thought):
>
> int i = 5;
> for (i = 0; i < 3; ++i)
> writefln(i); // 0 to 2
> writefln(i); // 2
>
> ie, the lack of a type makes the "i=0" an assignment, not a declaration with an initializer. The i from the surrounding scope is used. I expected foreach to behave the same way, but apparently it doesn't.
>
I guess it's the type inference that makes it so confusing to some. Earlier, after all, we had to write:
foreach (int i, char[] c; arr)
Instead of the modern terse version:
foreach (i, c; arr)
In the longer version, it's presumably more clear that the i is a new "int i".
|
June 08, 2006 Re: suggested change to foreach index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deewiant | Deewiant wrote:
> Sean Kelly wrote:
>
>>Deewiant wrote:
>>
>>>Sean Kelly wrote:
>>>
>>>> void main()
>>>> {
>>>> int i;
>>>> char[] arr = "abc def";
>>>>
>>>> foreach( i, c; arr )
>>>> {
>>>> printf( "%i\n", i );
>>>> if(' ' == c)
>>>> break;
>>>> }
>>>> printf( "\n%i\n", i );
>>>> }
>>>
>>>It creates a new scope, just like for loops:
>>>
>>>int i = 5;
>>>for (int i = 0; i < 3; ++i)
>>> writefln(i); // 0 to 2
>>>writefln(i); // 5
>>
>>Yes, but my example was equivalent to this (I thought):
>>
>> int i = 5;
>> for (i = 0; i < 3; ++i)
>> writefln(i); // 0 to 2
>> writefln(i); // 2
>>
>>ie, the lack of a type makes the "i=0" an assignment, not a declaration
>>with an initializer. The i from the surrounding scope is used. I
>>expected foreach to behave the same way, but apparently it doesn't.
>>
>
>
> I guess it's the type inference that makes it so confusing to some. Earlier,
> after all, we had to write:
>
> foreach (int i, char[] c; arr)
>
> Instead of the modern terse version:
>
> foreach (i, c; arr)
>
> In the longer version, it's presumably more clear that the i is a new "int i".
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){}
|
June 08, 2006 Re: suggested change to foreach index | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | "BCS" <BCS@pathlink.com> wrote in message news:e69lah$dmo$1@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. |
June 08, 2006 Re: suggested change to foreach index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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.
++votes; // fully agreed
--
Tomasz Stachowiak /+ a.k.a. h3r3tic +/
|
June 08, 2006 Re: suggested change to foreach index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | On Fri, 09 Jun 2006 06:49:11 +1000, Jarrett Billingsley <kb3ctd2@yahoo.com> wrote: > Or, more consistent, make auto foreach indices require "auto"? This will break some existing code but I think its worth it. It's a good idea. -- Derek Parnell Melbourne, Australia |
June 08, 2006 Re: suggested change to foreach index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> "BCS" <BCS@pathlink.com> wrote in message news:e69lah$dmo$1@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
|
June 10, 2006 Re: suggested change to foreach index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote: > "BCS" <BCS@pathlink.com> wrote in message news:e69lah$dmo$1@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. > > Agreed too, this is the natural/intuitive/consistent way to go. Those other solutions with "alias" and "inout" were awful. -- Bruno Medeiros - CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D |
June 10, 2006 Re: suggested change to foreach index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bruno Medeiros | 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>
|
June 10, 2006 Re: suggested change to foreach index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deewiant | In article <e6ev28$o1k$1@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? |
June 10, 2006 Re: suggested change to foreach index | ||||
---|---|---|---|---|
| ||||
Posted in reply to Deewiant | "Deewiant" <deewiant.doesnotlike.spam@gmail.com> wrote in message news:e6ev28$o1k$1@digitaldaemon.com... > 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. I'd really like something else for type inference too, but until that happens, "auto" would make the most sense. I think 'var' or something along those lines would be better; and it would be best if it functioned _as a type_ which would just stand for a "placeholder" type until the type could be determined in the semantic pass. > 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. Well, keep in mind that many of my proposals are based on my virulent hatred of most uses of type inference ;) It's bad enough when people start overusing auto; it's worse when it's not even obvious that a new variable is being declared as well! > Personally, I'd prefer "out" instead of "inout" or "alias", but that's just me. <g> That's good too, but the best would be if/when we get a true type-inference keyword for use with index inference, and then nothing when you want to use the outer scope's variable (to be consistent with the behavior of for loops). |
Copyright © 1999-2021 by the D Language Foundation