Fun surprising things

Stanislav Blinov stanislav.blinov at gmail.com
Wed Oct 31 17:28:43 UTC 2018


On Wednesday, 31 October 2018 at 15:00:26 UTC, Steven 
Schveighoffer wrote:
> 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.

Not in this case, no.

> 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.

Could, or could not. It's implementation-defined.

> 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?

You're misreading the spec. The relevant part is the one I 
quoted. There's no left-to-right evaluation rule here. It's not a 
binary expression, it's an argument list; in this case - one 
argument, which is a CatExpression, which is an AssignExpression, 
the order of evaluation of it's operands is 
implementation-defined.



More information about the Digitalmars-d mailing list