to auto or not to auto ( in foreach )

Lobelia Noakes via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 20 08:40:16 PDT 2016


On Sunday, 17 July 2016 at 01:58:59 UTC, pineapple wrote:
> On Sunday, 17 July 2016 at 01:57:21 UTC, pineapple wrote:
>> On Saturday, 16 July 2016 at 22:05:49 UTC, ketmar wrote:
>>> actually, `foreach (v; rng)` looks like `foreach` is 
>>> *reusing* *existing* *variable*. most of the time you can put 
>>> `immutable` or something like that there to note that it is 
>>> not reusing (purely cosmetical thing), but sometimes you 
>>> cannot, and then `auto` is perfect candidate... but it is not 
>>> allowed. (sigh)
>>
>> Chipping in my agreement. foreach(x; y) makes as much 
>> syntactic sense as for(x = 0; x < y; x++) where x was not 
>> previously defined. One does not expect something that does 
>> not look like every other variable definition in the language 
>> to be defining a new variable.
>
> Furthermore, if foreach(int x; y) is legal then why isn't 
> foreach(auto x; y)?

By the way there's an error in the grammar:

ForeachTypeAttribute:
     ref
     TypeCtor

But BasicType also already includes TypeCtor. So a TypeCtor in a 
foreach typelist is ? well hard to say, part of basic type or 
part of ForeachTypeAttribute ?


More information about the Digitalmars-d-learn mailing list