Evaluation order of index expressions

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon May 25 01:00:13 PDT 2015


On Monday, 25 May 2015 at 07:33:49 UTC, ketmar wrote:
> On Sun, 24 May 2015 19:30:52 +0000, kinke wrote:
>
>> So for the 2nd assignment's left-hand-side, the index is 
>> evaluated
>> before evaluating the container! Please don't tell me that's 
>> by design.
>> :>
>
> it is. at least this is what i was told when i faced the 
> similar issue.
> "WONTIFX, STFU".

To be fair, the example that the OP gave is almost the same thing 
as

foo(++i, ++i);

whereas what you came up with had a lot more layers to it, with 
whole chains of function calls affecting each other. With the 
kind of example you came up with, even with defining the 
evaluation as strictly left-to-right, you would _still_ run into 
screwy problems with stuff easily being mutated in a different 
order than you expected.

Defining the order of evaluation as being strictly left-to-right 
will avoid some of the common bugs cause by folks foolishly doing 
something like

foo(++i, ++i);

but the reality of the matter is, if you start doing stuff like 
mutating the arguments inside of the function inside of the 
function when the same arguments are being passed to other 
functions in the same expression, you _will_ have weird and 
unexpected stuff happening. It might be completely well-defined 
and consistent, but it may not be what you expect, and even if it 
is, a slight change to the code could change the order. So, the 
kind of stuff that you're complaining about not being able to do 
really shouldn't be done regardless of how well-defined the order 
of evaluation is. It's just begging for trouble.

- Jonathan M Davis


More information about the Digitalmars-d mailing list