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

Nick Treleaven nick at geany.org
Wed Aug 14 17:53:13 UTC 2019


On Tuesday, 13 August 2019 at 23:34:32 UTC, Manu wrote:
> On Tue, Aug 13, 2019 at 6:50 AM Dukc via Digitalmars-d 
> <digitalmars-d at puremagic.com> wrote:
>> >   static foreach (ref i; [10, 20, 30])
>> >       pragma(msg, __traits(isRef, i));
>> >
>> >> true true true
>>
>> Yes, these are the cases I meant with `static foreach`. I 
>> think `[10, 20, 30]` is a rvalue, and if the latter of these 
>> two examples compiles, it should not.
>
> Actually, because of D's 'weird shit' law, the array is not an 
> rvalue.
> (I think...?)

An array literal is an rvalue:

void f(ref int[] a);
void main(){
     f([10, 20, 30]); // error
}

error: cannot pass rvalue argument `[10, 20, 30]` of type `int[]` 
to parameter `ref int[] a`

>> > As above, we could allow this by creating temporaries the 
>> > same as function arguments... but we've decided not to allow 
>> > ref iterators from rvalues.
>> >
>> >   static foreach (auto ref i; [10, 20, 30])
>> >       pragma(msg, __traits(isRef, i));
>> >
>> >> false false false ??
>>
>> Yes, exactly what's supposed to happen.
>
> Right... what's your point?
> Incidentally, I'm not sure this will hold; because array 
> literals are
> not rvalues, so I think it might be `true true true` here...

An array literal is an rvalue, but indexing an array literal is 
an lvalue. So the above should print 'true' for each pragma.


More information about the Digitalmars-d mailing list