Skip to content

Commit 9a7ddf8

Browse files
committed
[expression.dd] Improve SliceExpression docs
1 parent 1dc8970 commit 9a7ddf8

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

spec/expression.dd

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,43 +1266,51 @@ $(GNAME Slice):
12661266
)
12671267

12681268
$(P $(I PostfixExpression) is evaluated.
1269-
if $(I PostfixExpression) is an expression of type
1270-
static array or dynamic array, the special variable $(DOLLAR)
1271-
is declared and set to be the length of the array.
1269+
If $(I PostfixExpression) is an expression of static array or
1270+
dynamic array type, the result of the slice is a dynamic array
1271+
of the element type of the $(I PostfixExpression).
1272+
)
1273+
1274+
$(P If $(I PostfixExpression) is a $(DDSUBLINK spec/template, variadic-templates, $(I ValueSeq)), then
1275+
the result of the slice is a new $(I ValueSeq) formed
1276+
from the upper and lower bounds, which must statically evaluate
1277+
to integral constants.
1278+
It is an error if those bounds are out of range.
1279+
)
1280+
1281+
$(P The special variable `$`
1282+
is declared and set to be the number of elements in the $(I PostfixExpression).
12721283
A new declaration scope is created for the evaluation of the
12731284
$(GLINK AssignExpression)..$(GLINK AssignExpression)
1274-
and $(DOLLAR) appears in that scope only.
1285+
and `$` appears in that scope only.
12751286
)
12761287

12771288
$(P The first $(I AssignExpression) is taken to be the inclusive
12781289
lower bound
12791290
of the slice, and the second $(I AssignExpression) is the
12801291
exclusive upper bound.
1281-
The result of the expression is a slice of the $(I PostfixExpression)
1282-
array.
1283-
)
1284-
1285-
$(P If the $(D [ ]) form is used, the slice is of the entire
1286-
array.
1292+
The result of the expression is a slice of the elements in $(I PostfixExpression).
12871293
)
12881294

1289-
$(P The type of the slice is a dynamic array of the element
1290-
type of the $(I PostfixExpression).
1295+
$(P If the $(D [ ]) form is used, the slice is of all the elements in $(I PostfixExpression).
12911296
)
12921297

12931298
$(P A $(I SliceExpression) is not a modifiable lvalue.)
12941299

1300+
$(H3 $(LNAME2 slice_to_static_array, Slice Conversion to Static Array))
1301+
12951302
$(P If the slice bounds can be known at compile time, the slice expression
1296-
is implicitly convertible to an lvalue of static array. For example:)
1303+
may be implicitly convertible to an lvalue of static array. For example:)
12971304

12981305
-------------
12991306
arr[a .. b] // typed T[]
13001307
-------------
13011308

1302-
If both $(CODE a) and $(CODE b) are integers (may be constant-folded),
1309+
If both $(CODE a) and $(CODE b) are integers (which may be constant-folded),
13031310
the slice expression can be converted to a static array type
13041311
$(D T[b - a]).
13051312

1313+
$(SPEC_RUNNABLE_EXAMPLE_RUN
13061314
-------------
13071315
void foo(int[2] a)
13081316
{
@@ -1327,9 +1335,10 @@ $(GNAME Slice):
13271335
bar(arr[1 .. 3]);
13281336
assert(arr == [1, 4, 5]);
13291337

1330-
//baz(arr[1 .. 3]); // cannot match length
1338+
//baz(arr[1 .. 3]); // cannot match length
13311339
}
13321340
-------------
1341+
)
13331342

13341343
$(P The following forms of slice expression can be convertible to a static array
13351344
type:)
@@ -1350,14 +1359,6 @@ type:)
13501359
$(TROW $(D arr[e-a .. e-b]), $(D a - b) $(I if) $(D a >= b))
13511360
)
13521361

1353-
$(P If $(I PostfixExpression) is a $(I ValueSeq), then
1354-
the result of the slice is a new $(I ValueSeq) formed
1355-
from the upper and lower bounds, which must statically evaluate
1356-
to integral constants.
1357-
It is an error if those
1358-
bounds are out of range.
1359-
)
1360-
13611362
$(H2 $(LNAME2 primary_expressions, Primary Expressions))
13621363

13631364
$(GRAMMAR

0 commit comments

Comments
 (0)