Fun surprising things

Steven Schveighoffer schveiguy at gmail.com
Wed Oct 31 15:00:26 UTC 2018


On 10/31/18 10:18 AM, Stanislav Blinov wrote:
> On Wednesday, 31 October 2018 at 14:00:14 UTC, bauss wrote:
> 
>> If your answer is "10" then you're wrong.
> 
> https://dlang.org/spec/expression.html#order-of-evaluation
> 
>> 2. Implementation Defined:
>>    The order of evaluation of the operands of AssignExpression.
>> ...
> 
> So indeed, both answers are "right".

No, the correct answer is "01", according to left to right evaluation.

a should be evaluated first, which returns 0, and increments c by 1.

Then b is evaluated, returning 1, and decrementing c by 1.

But the assembly shows that this binary expression is evaluated right to 
left, which is not what the spec says.

BUT, the lowering in the compiler could change arr1 ~ arr2 to a function 
call (arrayCat(arr1, arr2)), which then allows the compiler to evaluate 
in right to left order.

So the inconsistency in the spec ("binary expressions are left to 
right", "function arguments are implementation defined order") leaves a 
gaping hole. What happens when a binary expression lowers to a function 
call?

-Steve


More information about the Digitalmars-d mailing list