Evaluation order of index expressions

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Mon May 25 12:40:05 PDT 2015


On 05/24/2015 09:30 PM, 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]

A related issue is that the rewrites documented at 
http://dlang.org/operatoroverloading.html don't all preserve the order 
of subexpressions. However, ideally, the order of evaluation would be 
preserved.


More information about the Digitalmars-d mailing list