Slice expressions - exact evaluation order, dollar

Michael Coulombe via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 13 18:43:02 PDT 2016


On Friday, 17 June 2016 at 19:59:09 UTC, kinke wrote:
> // LDC issue #1433
> void main()
> {
>     auto r = getBase()[getLowerBound($) .. getUpperBound($)];
>     assert(r == [ 2, 3 ]);
> }
>
> Firstly, it fails with DMD 2.071 because $ in the upper bound 
> expression is 0, i.e., it doesn't reflect the updated length 
> (1) after evaluating the lower bound expression. LDC does.

The docs aren't fully detailed, but this is explicit behavior in 
the DMD front end that is the same no matter what type getBase() 
returns:

"Note that opDollar!i is only evaluated once for each i where $ 
occurs in the corresponding position in the indexing operation." 
- https://dlang.org/spec/operatoroverloading.html

"PostfixExpression is evaluated. if PostfixExpression is an 
expression of type static array or dynamic array, the special 
variable $ is declared and set to be the length of the array. " - 
https://dlang.org/spec/expression.html


More information about the Digitalmars-d mailing list