Skip to content

Commit 8550c6c

Browse files
committed
Document assigning a dynamic array to a static array
1 parent 05b16ff commit 8550c6c

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

spec/arrays.dd

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,9 @@ p = a.ptr; // p points to the first element of the array a.
185185
// error, since the length of the array pointed to by p is unknown
186186
//s = p;
187187

188-
s = [1,2]; // OK
189-
assert(s == [1,2,0]);
190-
s = [1,2,3];
191-
//s = [1,2,3,4]; // error, too many elements
192-
193188
//a = p; // error, length unknown
194189
a = s; // a points to the elements of s
195190
assert(a.ptr == s.ptr);
196-
assert(a == [1,2,3]);
197191

198192
int[] b;
199193
a = b; // a points to the same array as b does
@@ -205,7 +199,29 @@ assert(a == []);
205199
using pointer $(RELATIVE_LINK2 slicing, slicing), so that the number of elements
206200
to copy is then known.)
207201

208-
$(P See also $(RELATIVE_LINK2 array-copying, Copying).)
202+
$(P A static array can be assigned from a dynamic array. The lengths must match:)
203+
204+
$(SPEC_RUNNABLE_EXAMPLE_RUN
205+
---
206+
int[3] s;
207+
int[] a;
208+
209+
//s = [1, 2]; // error
210+
s = [1, 2, 3]; // OK
211+
//s = [1, 2, 3, 4]; // error
212+
213+
a = [4, 5, 6];
214+
s = a; // OK
215+
a = [1, 2];
216+
//s = a; // RangeError
217+
218+
a = s;
219+
//s = a; // RangeError, overlap
220+
---
221+
)
222+
$(P The dynamic array data must not $(RELATIVE_LINK2 overlapping-copying, overlap)
223+
with the static array memory.
224+
See also $(RELATIVE_LINK2 array-copying, Copying).)
209225

210226

211227
$(H2 $(LNAME2 indexing, Indexing))

0 commit comments

Comments
 (0)