Evaluation order of index expressions

kinke via Digitalmars-d digitalmars-d at puremagic.com
Sun May 24 13:14:26 PDT 2015


On Sunday, 24 May 2015 at 19:48:05 UTC, Jonathan M Davis wrote:
> The original code is clearly wrong. And forcing the order of 
> evaluation so that it's one way or the other just changes under 
> which cases you end up with bugs. Mutating in an expression 
> while using it multiple times in that expression or mutating a 
> variable in an expression while using a variable that depends 
> on it is just plain error-prone and is a serious code smell.
>
> I really don't see anything wrong with what the compiler is 
> doing in this case. The problem is that the code was making bad 
> assumptions.

We agree on the original code smell.

I think the evaluation order should be well-defined by the 
language though, following the intuitive left-to-right order for 
cases like this. Left-hand-side before right-hand-side in assign 
statements, container before its index/index range, for the 
latter start before end etc.
Then at least all compilers of that language exhibit the same 
behavior and we don't end up with cases like this, where LDC 
complains and DMD compiles. Even worse would be not-so-obvious 
side effects caused by differing evaluation orders of different 
compilers, with a fair potential for nasty bugs.


More information about the Digitalmars-d mailing list