Skip to content

Commit 471a2d7

Browse files
committed
Fix slice to static array implicit conversion docs
Show implicit conversion, not assignment in examples. Hide unimplemented conversions from runtime indexed slice of known length - the docs were merged but the compiler pull was not merged: dlang/dmd#4209 (comment)
1 parent 467f934 commit 471a2d7

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

spec/expression.dd

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,23 +1511,30 @@ $(GNAME Slice):
15111511
$(H3 $(LNAME2 slice_to_static_array, Slice Conversion to Static Array))
15121512

15131513
$(P If the slice bounds can be known at compile time, the slice expression
1514-
may be implicitly convertible to an lvalue of static array. For example:)
1514+
may be implicitly convertible to a static array lvalue. For example:)
15151515

15161516
-------------
15171517
arr[a .. b] // typed T[]
15181518
-------------
15191519

1520+
$(P
15201521
If both $(CODE a) and $(CODE b) are integers (which may be constant-folded),
15211522
the slice expression can be converted to a static array of type
15221523
$(D T[b - a]).
1524+
)
1525+
$(NOTE a static array can also be $(DDSUBLINK spec/arrays, assignment,
1526+
assigned from a slice), performing a runtime check that the lengths match.)
15231527

1524-
$(SPEC_RUNNABLE_EXAMPLE_RUN
1528+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
15251529
---
1530+
void f(int[2] sa);
1531+
15261532
int[] arr = [1, 2, 3];
1527-
int[2] sa = arr[1 .. 3];
1533+
//f(arr); // error, can't convert
1534+
f(arr[1 .. 3]); // OK
1535+
//f(arr[0 .. 3]); // error
15281536

1529-
assert(sa == [2, 3]);
1530-
//sa = arr[0 .. 3]; // error, cannot match length
1537+
int[2] g() { return arr[0 .. 2]; }
15311538
---
15321539
)
15331540

@@ -1550,16 +1557,17 @@ $(H3 $(LNAME2 slice_to_static_array, Slice Conversion to Static Array))
15501557
-------------
15511558
)
15521559

1560+
$(COMMENT Not implemented yet - https://issues.dlang.org/show_bug.cgi?id=13700
15531561
$(P Certain other forms of slice expression can be implicitly converted to a static array
1554-
when the slice length can be known at compile-time.
1562+
when the slice length can be known at compile-time.)
15551563

15561564
$(SPEC_RUNNABLE_EXAMPLE_RUN
15571565
-------------
15581566
int[] da = [1, 2, 3];
15591567
int i = da[0]; // runtime variable
15601568

1561-
int[2] sa = da[i .. i + 2];
1562-
assert(sa == [2, 3]);
1569+
int[2] f() { return da[i .. i + 2]; }
1570+
assert(f() == [2, 3]);
15631571
-------------
15641572
)
15651573

@@ -1580,6 +1588,7 @@ $(H3 $(LNAME2 slice_to_static_array, Slice Conversion to Static Array))
15801588
$(TROW $(D arr[e+a .. e+b]), $(D b - a) $(I if) $(D a <= b))
15811589
$(TROW $(D arr[e-a .. e-b]), $(D a - b) $(I if) $(D a >= b))
15821590
)
1591+
)
15831592

15841593
$(H2 $(LNAME2 primary_expressions, Primary Expressions))
15851594

0 commit comments

Comments
 (0)