Evaluation order of index expressions

Iain Buclaw via Digitalmars-d digitalmars-d at puremagic.com
Sun May 24 23:13:20 PDT 2015


On 25 May 2015 01:10, "Timon Gehr via Digitalmars-d" <
digitalmars-d at puremagic.com> wrote:
>
> On 05/25/2015 12:36 AM, Iain Buclaw via Digitalmars-d wrote:
>>
>>
>>  > This comes up once in a while. We should stick with left to right
>> through and through. It's a "simple" matter of getting somebody on the
>> compiler team to find the time for it. -- Andrei
>>  >
>>
>> I find it is not as clear cut as that.  In gdc, there is a compiler flag
>> that tells the optimizer to honour left to right evaluation, and because
>> of both what you say and the agreement of others, it seems natural to
>> have this turned on by default.
>>
>> However, this has an interesting side effect with operations with side
>> effects.  Ie: foo += bar() could either produce expected or surprising
>> results.
>>
>> Hint, the LTR order - foo = foo + bar() - gives the most surprise in my
>> experience from users.
>>
>
> I think I still don't get it. What is the surprise? That bar() is
evaluated before the value is written to foo?

That foo is cached before bar() is evaluated.

The context here involves concurrency where bar() calls yield and makes
changes to foo before returning to assign the updated results.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20150525/861063c2/attachment.html>


More information about the Digitalmars-d mailing list