Skip to content

Commit 703033e

Browse files
authored
Merge pull request #3160 from ntrel/expr-slice
[expression.dd] Improve IndexExpression & SliceExpression docs Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents 60b18e4 + 79d10c8 commit 703033e

File tree

1 file changed

+49
-32
lines changed

1 file changed

+49
-32
lines changed

spec/expression.dd

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,22 +1341,30 @@ $(GNAME IndexExpression):
13411341
$(GLINK PostfixExpression) $(D [) $(GLINK ArgumentList) $(D ])
13421342
)
13431343

1344-
$(P $(I PostfixExpression) is evaluated. If $(I PostfixExpression) is an
1345-
expression of type static array or dynamic array, the symbol $(DOLLAR) is set to
1346-
be the number of elements in the array. If $(I PostfixExpression) is a $(I
1347-
ValueSeq), the symbol $(DOLLAR) is set to be the number of elements
1348-
in the sequence. A new declaration scope is created for the evaluation of the
1349-
$(GLINK ArgumentList) and $(DOLLAR) appears in that scope only.)
1350-
1351-
$(P If $(I PostfixExpression) is a $(I ValueSeq),
1352-
then the $(GLINK ArgumentList) must consist of only one argument,
1344+
$(P $(I PostfixExpression) is evaluated.
1345+
If $(I PostfixExpression) is an expression of static or
1346+
dynamic array type, the result of the indexing is an lvalue
1347+
of the *i*th element in the array, where `i` is an integer
1348+
evaluated from $(I ArgumentList).
1349+
If $(I PostfixExpression) is a pointer `p`, the result is
1350+
`*(p + i)` (see $(RELATIVE_LINK2 pointer_arithmetic, Pointer Arithmetic)).
1351+
)
1352+
1353+
$(P If $(I PostfixExpression) is a $(DDSUBLINK spec/template, variadic-templates, $(I ValueSeq))
1354+
then the $(I ArgumentList) must consist of only one argument,
13531355
and that must be statically evaluatable to an integral constant.
13541356
That integral constant $(I n) then selects the $(I n)th
13551357
expression in the $(I ValueSeq), which is the result
13561358
of the $(I IndexExpression).
13571359
It is an error if $(I n) is out of bounds of the $(I ValueSeq).
13581360
)
13591361

1362+
$(P The special variable `$` is declared and set to be the number
1363+
of elements in the $(I PostfixExpression) (when available).
1364+
A new declaration scope is created for the evaluation of the
1365+
$(I ArgumentList) and `$` appears in that scope only.
1366+
)
1367+
13601368
$(H2 $(LNAME2 slice_expressions, Slice Expressions))
13611369

13621370
$(GRAMMAR
@@ -1372,43 +1380,59 @@ $(GNAME Slice):
13721380
)
13731381

13741382
$(P $(I PostfixExpression) is evaluated.
1375-
if $(I PostfixExpression) is an expression of type
1376-
static array or dynamic array, the special variable $(DOLLAR)
1377-
is declared and set to be the length of the array.
1378-
A new declaration scope is created for the evaluation of the
1379-
$(GLINK AssignExpression)..$(GLINK AssignExpression)
1380-
and $(DOLLAR) appears in that scope only.
1383+
If $(I PostfixExpression) is a static or dynamic
1384+
array `a`, the result of the slice is a dynamic array
1385+
referencing elements `a[i]` to `a[j-1]` inclusive, where `i`
1386+
and `j` are integers evaluated from the first and second $(I
1387+
AssignExpression) respectively.
1388+
)
1389+
1390+
$(P If $(I PostfixExpression) is a pointer `p`, the result
1391+
will be a dynamic array referencing elements from `p[i]` to `p[j-1]`
1392+
inclusive, where `i` and `j` are integers evaluated from the
1393+
first and second $(I AssignExpression) respectively.
1394+
)
1395+
1396+
$(P If $(I PostfixExpression) is a $(DDSUBLINK spec/template, variadic-templates, $(I ValueSeq)), then
1397+
the result of the slice is a new $(I ValueSeq) formed
1398+
from the upper and lower bounds, which must statically evaluate
1399+
to integral constants.
1400+
It is an error if those bounds are out of range.
13811401
)
13821402

13831403
$(P The first $(I AssignExpression) is taken to be the inclusive
13841404
lower bound
13851405
of the slice, and the second $(I AssignExpression) is the
13861406
exclusive upper bound.
1387-
The result of the expression is a slice of the $(I PostfixExpression)
1388-
array.
1407+
The result of the expression is a slice of the elements in $(I PostfixExpression).
13891408
)
13901409

1391-
$(P If the $(D [ ]) form is used, the slice is of the entire
1392-
array.
1410+
$(P The special variable `$` is declared and set to be the number
1411+
of elements in the $(I PostfixExpression) (when available).
1412+
A new declaration scope is created for the evaluation of the
1413+
$(I AssignExpression)`..`$(I AssignExpression) and `$` appears in
1414+
that scope only.
13931415
)
13941416

1395-
$(P The type of the slice is a dynamic array of the element
1396-
type of the $(I PostfixExpression).
1417+
$(P If the $(D [ ]) form is used, the slice is of all the elements in $(I PostfixExpression).
13971418
)
13981419

13991420
$(P A $(I SliceExpression) is not a modifiable lvalue.)
14001421

1422+
$(H3 $(LNAME2 slice_to_static_array, Slice Conversion to Static Array))
1423+
14011424
$(P If the slice bounds can be known at compile time, the slice expression
1402-
is implicitly convertible to an lvalue of static array. For example:)
1425+
may be implicitly convertible to an lvalue of static array. For example:)
14031426

14041427
-------------
14051428
arr[a .. b] // typed T[]
14061429
-------------
14071430

1408-
If both $(CODE a) and $(CODE b) are integers (may be constant-folded),
1431+
If both $(CODE a) and $(CODE b) are integers (which may be constant-folded),
14091432
the slice expression can be converted to a static array type
14101433
$(D T[b - a]).
14111434

1435+
$(SPEC_RUNNABLE_EXAMPLE_RUN
14121436
-------------
14131437
void foo(int[2] a)
14141438
{
@@ -1433,9 +1457,10 @@ $(GNAME Slice):
14331457
bar(arr[1 .. 3]);
14341458
assert(arr == [1, 4, 5]);
14351459

1436-
//baz(arr[1 .. 3]); // cannot match length
1460+
//baz(arr[1 .. 3]); // cannot match length
14371461
}
14381462
-------------
1463+
)
14391464

14401465
$(P The following forms of slice expression can be convertible to a static array
14411466
type:)
@@ -1456,14 +1481,6 @@ type:)
14561481
$(TROW $(D arr[e-a .. e-b]), $(D a - b) $(I if) $(D a >= b))
14571482
)
14581483

1459-
$(P If $(I PostfixExpression) is a $(I ValueSeq), then
1460-
the result of the slice is a new $(I ValueSeq) formed
1461-
from the upper and lower bounds, which must statically evaluate
1462-
to integral constants.
1463-
It is an error if those
1464-
bounds are out of range.
1465-
)
1466-
14671484
$(H2 $(LNAME2 primary_expressions, Primary Expressions))
14681485

14691486
$(GRAMMAR

0 commit comments

Comments
 (0)