Evaluation order of index expressions

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Sun May 24 13:29:58 PDT 2015


On 05/24/2015 09:48 PM, Jonathan M Davis wrote:
> On Sunday, 24 May 2015 at 19:30:54 UTC, kinke wrote:
>> <code>
>> import core.stdc.stdio;
>>
>> static int[] _array = [ 0, 1, 2, 3 ];
>>
>> int[] array() @property { printf("array()\n"); return _array; }
>> int   start() @property { printf("start()\n"); return 0; }
>> int   end()   @property { printf("end()\n");   return 1; }
>>
>> void main()
>> {
>>     array[start..end] = 666;
>>     printf("---\n");
>>     array[start] = end;
>> }
>> </code>
>>
>> <stdout>
>> array()
>> start()
>> end()
>> ---
>> start()
>> array()
>> end()
>> </stdout>
>>
>> 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. :>
>>
>> [origin: https://github.com/D-Programming-Language/phobos/pull/3311]
>
> Why would you expect the order to even be defined?

Because this is not C.

BTW, the documentation contradicts itself on evaluation order: 
http://dlang.org/expression.html


More information about the Digitalmars-d mailing list