@@ -1466,48 +1466,59 @@ $(H3 $(LNAME2 slice_to_static_array, Slice Conversion to Static Array))
1466
1466
-------------
1467
1467
1468
1468
If both $(CODE a) and $(CODE b) are integers (which may be constant-folded),
1469
- the slice expression can be converted to a static array type
1469
+ the slice expression can be converted to a static array of type
1470
1470
$(D T[b - a]).
1471
1471
1472
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1473
+ ---
1474
+ int[] arr = [1, 2, 3];
1475
+ int[2] sa = arr[1 .. 3];
1476
+
1477
+ assert(sa == [2, 3]);
1478
+ //sa = arr[0 .. 3]; // error, cannot match length
1479
+ ---
1480
+ )
1481
+
1472
1482
$(SPEC_RUNNABLE_EXAMPLE_RUN
1473
1483
-------------
1474
- void foo(int[2] a)
1475
- {
1476
- assert(a == [2, 3]);
1477
- }
1478
1484
void bar(ref int[2] a)
1479
1485
{
1480
1486
assert(a == [2, 3]);
1481
- a[0] = 4;
1482
- a[1] = 5;
1483
- assert(a == [4, 5]);
1487
+ a = [4, 5];
1484
1488
}
1485
- void baz(int[3] a) {}
1486
1489
1487
1490
void main()
1488
1491
{
1489
1492
int[] arr = [1, 2, 3];
1490
1493
1491
- foo(arr[1 .. 3]);
1492
- assert(arr == [1, 2, 3]);
1493
-
1494
+ // slicing an lvalue gives an lvalue
1494
1495
bar(arr[1 .. 3]);
1495
1496
assert(arr == [1, 4, 5]);
1496
-
1497
- //baz(arr[1 .. 3]); // cannot match length
1498
1497
}
1499
1498
-------------
1500
1499
)
1501
1500
1502
- $(P The following forms of slice expression can be convertible to a static array
1503
- type:)
1501
+ $(P Certain other forms of slice expression can be implicitly converted to a static array
1502
+ when the slice length can be known at compile-time.
1503
+
1504
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1505
+ -------------
1506
+ int[] da = [1, 2, 3];
1507
+ int i = da[0]; // runtime variable
1508
+
1509
+ int[2] sa = da[i .. i + 2];
1510
+ assert(sa == [2, 3]);
1511
+ -------------
1512
+ )
1513
+
1514
+ $(P The table below shows all the forms recognized:)
1504
1515
1505
1516
$(DL
1506
1517
$(DT $(D e)) $(DD An expression that contains no side effects.)
1507
1518
$(DT $(D a), $(D b)) $(DD Integers (that may be constant-folded).)
1508
1519
)
1509
1520
1510
- $(TABLE2 Computing array lengths during compilation ,
1521
+ $(TABLE2 ,
1511
1522
$(THEAD Form, The length calculated at compile time)
1512
1523
$(TROW $(D arr[]), The compile time length of $(D arr) if it's known.)
1513
1524
$(TROW $(D arr[a .. b]), $(D b - a))
0 commit comments