DIP 1022--foreach auto ref--Community Review Round 1

Dukc ajieskola at gmail.com
Sat Aug 17 07:40:47 UTC 2019


On Friday, 16 August 2019 at 05:31:38 UTC, Manu wrote:
> On Thu, Aug 15, 2019 at 1:16 AM Dukc via Digitalmars-d
>> If it is not, sorry, I still don't get it.
>
>         void fun(ref int x)
>         {
>             auto y = x; // remember x
>
>             foreach (i; AliasSeq!(x, x))
>             {
>                 ++i;
>             }
>             assert(x == y); // only copies were incremented, x 
> is unchanged
>
>             static foreach (i; AliasSeq!(x, x))
>             {
>                 ++i;
>             }
>             assert(x == y + 2); // the aliased symbol 'x' was 
> incremented
>         }
>

Yes, this works. Disregard all what I have said about `static 
foreach` over an `AliasSeq`, I obviously need to think it 
completely again.

>>
>> Okay, I'll say that `static foreach` with `auto ref` has no 
>> effect as long as compile-time lvalues are not possible, but 
>> should change the behaviour accordingly if they are possible 
>> in the future.
>
> Compile-time lvalues are absolutely possible, I've showed code 
> how it works a bunch of times, including just above (difference 
> between foreach and static foreach).
>

Yeah, as an `AliasSeq`, but I meant ranges and structs with an 
`opApply()` here. I know I didn't say it that way in the DIP, but 
I already agreed to change it.

>> And remember, this clause does not apply to alias sequences.
>
> It's a bit weird to say 'except' that way; static foreach 
> primarily applies to tuples, and foreach should probably be 
> deprecated such that it can't apply to tuples.
>
> It's not technically wrong, but it's just seems a bit of a 
> weird way to paint that rule to me.

I'll try to avoid describing it this way in the DIP, it's just 
that it was easiest to answer quote-by-quote in this theard and 
that led to my articulation of "except".



More information about the Digitalmars-d mailing list