Skip to content

Commit 3add73c

Browse files
authored
Merge pull request #3404 from ntrel/array-docs
[spec/arrays] Improve assignment docs Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents c902cfa + e9e4536 commit 3add73c

File tree

1 file changed

+76
-27
lines changed

1 file changed

+76
-27
lines changed

spec/arrays.dd

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ $(H2 $(LNAME2 assignment, Array Assignment))
127127
the handle for these types.
128128
)
129129

130+
$(P The `.ptr` property for static and dynamic arrays will give the address
131+
of the first element in the array:)
132+
130133
$(SPEC_RUNNABLE_EXAMPLE_RUN
131134
---------
132135
int* p;
@@ -138,24 +141,49 @@ p = a.ptr; // p points to the first element of the array a.
138141

139142
// error, since the length of the array pointed to by p is unknown
140143
//s = p;
141-
//s = a; // error, as a's length is not known at compile-time
142-
s = [1,2,3];
143144

144145
//a = p; // error, length unknown
145146
a = s; // a points to the elements of s
146147
assert(a.ptr == s.ptr);
147-
assert(a == [1,2,3]);
148148

149149
int[] b;
150150
a = b; // a points to the same array as b does
151151
assert(a.ptr == b.ptr);
152152
assert(a == []);
153153
---------
154154
)
155-
$(P Each of the three error lines above can be made to copy elements
156-
using $(RELATIVE_LINK2 slicing, slicing), so that the number of elements
155+
$(NOTE The two error lines above can be made to copy elements
156+
using pointer $(RELATIVE_LINK2 slicing, slicing), so that the number of elements
157157
to copy is then known.)
158158

159+
$(P A static array can be assigned from a dynamic array - the data is copied.
160+
The lengths must match:)
161+
162+
$(SPEC_RUNNABLE_EXAMPLE_RUN
163+
---
164+
int[3] s;
165+
int[] a;
166+
167+
//s = [1, 2]; // error
168+
s = [1, 2, 3]; // OK
169+
//s = [1, 2, 3, 4]; // error
170+
171+
a = [4, 5, 6];
172+
s = a; // OK
173+
assert(s.ptr != a.ptr);
174+
a = [1, 2];
175+
//s = a; // RangeError, length mismatch
176+
177+
a = s;
178+
assert(a.ptr == s.ptr);
179+
//s = a; // RangeError, overlap
180+
---
181+
)
182+
$(P The dynamic array data must not $(RELATIVE_LINK2 overlapping-copying, overlap)
183+
with the static array memory.
184+
See also $(RELATIVE_LINK2 array-copying, Copying).)
185+
186+
159187
$(H2 $(LNAME2 indexing, Indexing))
160188

161189
$(P Indexing allows access to an element of an array:)
@@ -205,7 +233,7 @@ $(H2 $(LNAME2 slicing, Slicing))
205233

206234
$(P $(I Slicing) an array means to specify a subarray of it.
207235
An array slice does not copy the data, it is only another
208-
reference to it.
236+
reference to it. Slicing produces a dynamic array.
209237
For example:
210238
)
211239

@@ -291,18 +319,26 @@ $(H2 $(LNAME2 array-copying, Array Copying))
291319
right-hand side is an array of or pointer to the same type.
292320
)
293321

294-
$(SPEC_RUNNABLE_EXAMPLE_FAIL
322+
$(SPEC_RUNNABLE_EXAMPLE_RUN
295323
---------
296-
int[3] s;
297-
int[3] t;
324+
int[3] s, t;
325+
int[] a;
298326

299327
s = t; // the 3 elements of t are copied into s
300328
s[] = t; // the 3 elements of t are copied into s
301329
s[] = t[]; // the 3 elements of t are copied into s
302330
s[1..2] = t[0..1]; // same as s[1] = t[0]
303331
s[0..2] = t[1..3]; // same as s[0] = t[1], s[1] = t[2]
304-
s[0..4] = t[0..4]; // error, only 3 elements in s
305-
s[0..2] = t; // error, operands have different lengths
332+
//s[0..4] = t[0..4]; // error, only 3 elements in s and t
333+
//s[0..2] = t; // error, operands have different lengths
334+
335+
a = [1, 2];
336+
s[0..2] = a;
337+
assert(s == [1, 2, 0]);
338+
339+
//a[] = s; // RangeError, lengths don't match
340+
a[0..2] = s[1..3];
341+
assert(a == [2, 0]);
306342
---------
307343
)
308344

@@ -347,19 +383,28 @@ $(H2 $(LNAME2 array-setting, Array Setting))
347383
left-hand side are set to the right-hand side.
348384
)
349385

350-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
386+
$(SPEC_RUNNABLE_EXAMPLE_RUN
351387
---------
352388
int[3] s;
389+
int[] a;
353390
int* p;
354391

355-
s[] = 3; // same as s[0] = 3, s[1] = 3, s[2] = 3
356-
p[0..2] = 3; // same as p[0] = 3, p[1] = 3
392+
s[] = 3;
393+
assert(s == [3, 3, 3]);
394+
395+
a = s;
396+
a[] = 1;
397+
assert(s == [1, 1, 1]);
398+
399+
p = s.ptr;
400+
p[0..2] = 2;
401+
assert(s == [2, 2, 1]);
357402
---------
358403
)
359404

360405
$(H2 $(LNAME2 array-concatenation, Array Concatenation))
361406

362-
$(P The binary operator ~ is the $(I cat) operator. It is used
407+
$(P The binary operator `~` is the $(I cat) operator. It is used
363408
to concatenate arrays:
364409
)
365410

@@ -373,19 +418,19 @@ assert(b == [1, 2, 3, 4]); // concatenate two arrays
373418
---
374419
)
375420

376-
$(P Many languages overload the + operator for concatenation.
421+
$(P Many languages overload the `+` operator for concatenation.
377422
This confusingly leads to a dilemma - does:
378423
)
379424

380425
---------
381426
"10" + 3 + 4
382427
---------
383428

384-
$(P produce the number 17, the string "1034" or the string "107" as the
429+
$(P produce the number `17`, the string `"1034"` or the string `"107"` as the
385430
result? It isn't obvious, and the language designers wind up carefully
386431
writing rules to disambiguate it - rules that get incorrectly
387432
implemented, overlooked, forgotten, and ignored. It's much better to
388-
have + mean addition, and a separate operator to be array
433+
have `+` mean addition, and a separate operator to be array
389434
concatenation.
390435
)
391436

@@ -409,7 +454,7 @@ assert(a == b);
409454

410455
$(H2 $(LNAME2 array-appending, Array Appending))
411456

412-
$(P Similarly, the ~= operator means append, as in:
457+
$(P Similarly, the `~=` operator means append, as in:
413458
)
414459

415460
---------
@@ -588,7 +633,7 @@ a.dup; // creates an array of a.length elements, copies
588633
$(H3 $(LNAME2 resize, Setting Dynamic Array Length))
589634

590635
$(P The $(D .length) property of a dynamic array can be set
591-
as the left-hand side of an = operator:
636+
as the left-hand side of an `=` operator:
592637
)
593638

594639
---------
@@ -827,7 +872,7 @@ $(H3 $(LNAME2 default-initialization, Default Initialization))
827872
$(H3 $(LNAME2 length-initialization, Length Initialization))
828873
$(P The $(D new) expression can be used to start a dynamic array
829874
with a specified length by specifying its type and then using the
830-
() syntax:
875+
`(size)` syntax:
831876
)
832877

833878
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -846,36 +891,40 @@ $(H3 $(LNAME2 void-initialization, Void Initialization))
846891
Void initializations are an advanced technique and should only be used
847892
when profiling indicates that it matters.
848893
)
849-
$(P to void initialise the elements of dynamic array use
894+
$(P To void initialise the *elements* of a dynamic array use
850895
$(REF uninitializedArray, std,array).
851896
)
852897

853898

854899
$(H3 $(LNAME2 static-init-static, Static Initialization of Statically Allocated Arrays))
855900

856901
$(P Static initalizations are supplied by a list of array
857-
element values enclosed in [ ]. The values can be optionally
858-
preceded by an index and a :.
902+
element values enclosed in `[ ]`. The values can be optionally
903+
preceded by an index and a `:`.
859904
If an index is not supplied, it is set to the previous index
860905
plus 1, or 0 if it is the first value.
861906
)
862907

863-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
908+
$(SPEC_RUNNABLE_EXAMPLE_RUN
864909
---------
865910
int[3] a = [ 1:2, 3 ]; // a[0] = 0, a[1] = 2, a[2] = 3
911+
912+
assert(a == [0, 2, 3]);
866913
---------
867914
)
868915

869-
$(P This is most handy when the array indices are given by enums:)
916+
$(P This is most handy when the array indices are given by $(DDLINK spec/enum, Enums, enums):)
870917

871-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
918+
$(SPEC_RUNNABLE_EXAMPLE_RUN
872919
---------
873920
enum Color { red, blue, green }
874921

875922
int[Color.max + 1] value =
876923
[ Color.blue :6,
877924
Color.green:2,
878925
Color.red :5 ];
926+
927+
assert(value == [5, 6, 2]);
879928
---------
880929
)
881930

0 commit comments

Comments
 (0)