assignment: left-to-right or right-to-left evaluation?
Georg Wrede
georg.wrede at iki.fi
Mon May 11 09:21:00 PDT 2009
Andrei Alexandrescu wrote:
> Georg Wrede wrote:
>> Andrei Alexandrescu wrote:
>>> Consider:
>>>
>>> uint fun();
>>> int gun();
>>> ...
>>> int[] a = new int[5];
>>> a[fun] = gun;
>>>
>>> Which should be evaluated first, fun() or gun()?
>>
>> arra[i] = arrb[i++];
>>
>> arra[i++] = arrb[i];
>>
>> I'm not sure that such dependences are good code.
>>
>> By stating a definite order between lvalue and rvalue, you would
>> actually encourage this kind of code.
>
> By not stating it, I introduce a gratuitous nonportability.
If the programmer has introduced dependencies on the evaluation order,
yes. But if he hasn't, then it will not introduce anything.
With
a[fun] = gun;
a rewrite
auto f = a[fun];
a[f] = gun;
makes it explicit how the programmer wants it done. It also removes any
uncertainty (and need to remember an arbitrary rule) for other people.
If you'd really want things easy for Walter, unambiguous, and clear for
the reader, then you'd advocate forbidding expressions in lvalues.
More information about the Digitalmars-d
mailing list