Skip to content

Commit 05b16ff

Browse files
committed
[spec/arrays] Tweak docs
Introduce .ptr property before example using it. Fix wrong example showing `s = a` as an error. Show assigning a shorter array literal to a static array is OK. Improve Array Setting example.
1 parent 751bbc7 commit 05b16ff

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

spec/arrays.dd

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ $(H2 $(LNAME2 assignment, Array Assignment))
170170
the handle for these types.
171171
)
172172

173+
$(P The `.ptr` property for static and dynamic arrays will give the address
174+
of the first element in the array:)
175+
173176
$(SPEC_RUNNABLE_EXAMPLE_RUN
174177
---------
175178
int* p;
@@ -181,8 +184,11 @@ p = a.ptr; // p points to the first element of the array a.
181184

182185
// error, since the length of the array pointed to by p is unknown
183186
//s = p;
184-
//s = a; // error, as a's length is not known at compile-time
187+
188+
s = [1,2]; // OK
189+
assert(s == [1,2,0]);
185190
s = [1,2,3];
191+
//s = [1,2,3,4]; // error, too many elements
186192

187193
//a = p; // error, length unknown
188194
a = s; // a points to the elements of s
@@ -195,10 +201,13 @@ assert(a.ptr == b.ptr);
195201
assert(a == []);
196202
---------
197203
)
198-
$(P Each of the three error lines above can be made to copy elements
199-
using $(RELATIVE_LINK2 slicing, slicing), so that the number of elements
204+
$(NOTE The two error lines above can be made to copy elements
205+
using pointer $(RELATIVE_LINK2 slicing, slicing), so that the number of elements
200206
to copy is then known.)
201207

208+
$(P See also $(RELATIVE_LINK2 array-copying, Copying).)
209+
210+
202211
$(H2 $(LNAME2 indexing, Indexing))
203212

204213
$(P Indexing allows access to an element of an array:)
@@ -248,7 +257,7 @@ $(H2 $(LNAME2 slicing, Slicing))
248257

249258
$(P $(I Slicing) an array means to specify a subarray of it.
250259
An array slice does not copy the data, it is only another
251-
reference to it.
260+
reference to it. Slicing produces a dynamic array.
252261
For example:
253262
)
254263

@@ -390,19 +399,28 @@ $(H2 $(LNAME2 array-setting, Array Setting))
390399
left-hand side are set to the right-hand side.
391400
)
392401

393-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
402+
$(SPEC_RUNNABLE_EXAMPLE_RUN
394403
---------
395404
int[3] s;
405+
int[] a;
396406
int* p;
397407

398-
s[] = 3; // same as s[0] = 3, s[1] = 3, s[2] = 3
399-
p[0..2] = 3; // same as p[0] = 3, p[1] = 3
408+
s[] = 3;
409+
assert(s == [3, 3, 3]);
410+
411+
a = s;
412+
a[] = 1;
413+
assert(s == [1, 1, 1]);
414+
415+
p = s.ptr;
416+
p[0..2] = 2;
417+
assert(s == [2, 2, 1]);
400418
---------
401419
)
402420

403421
$(H2 $(LNAME2 array-concatenation, Array Concatenation))
404422

405-
$(P The binary operator ~ is the $(I cat) operator. It is used
423+
$(P The binary operator `~` is the $(I cat) operator. It is used
406424
to concatenate arrays:
407425
)
408426

@@ -416,19 +434,19 @@ assert(b == [1, 2, 3, 4]); // concatenate two arrays
416434
---
417435
)
418436

419-
$(P Many languages overload the + operator for concatenation.
437+
$(P Many languages overload the `+` operator for concatenation.
420438
This confusingly leads to a dilemma - does:
421439
)
422440

423441
---------
424442
"10" + 3 + 4
425443
---------
426444

427-
$(P produce the number 17, the string "1034" or the string "107" as the
445+
$(P produce the number `17`, the string `"1034"` or the string `"107"` as the
428446
result? It isn't obvious, and the language designers wind up carefully
429447
writing rules to disambiguate it - rules that get incorrectly
430448
implemented, overlooked, forgotten, and ignored. It's much better to
431-
have + mean addition, and a separate operator to be array
449+
have `+` mean addition, and a separate operator to be array
432450
concatenation.
433451
)
434452

@@ -452,7 +470,7 @@ assert(a == b);
452470

453471
$(H2 $(LNAME2 array-appending, Array Appending))
454472

455-
$(P Similarly, the ~= operator means append, as in:
473+
$(P Similarly, the `~=` operator means append, as in:
456474
)
457475

458476
---------
@@ -631,7 +649,7 @@ a.dup; // creates an array of a.length elements, copies
631649
$(H3 $(LNAME2 resize, Setting Dynamic Array Length))
632650

633651
$(P The $(D .length) property of a dynamic array can be set
634-
as the left-hand side of an = operator:
652+
as the left-hand side of an `=` operator:
635653
)
636654

637655
---------
@@ -854,7 +872,7 @@ $(H3 $(LNAME2 default-initialization, Default Initialization))
854872
$(H3 $(LNAME2 length-initialization, Length Initialization))
855873
$(P The $(D new) expression can be used to start a dynamic array
856874
with a specified length by specifying its type and then using the
857-
() syntax:
875+
`(size)` syntax:
858876
)
859877

860878
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -873,36 +891,40 @@ $(H3 $(LNAME2 void-initialization, Void Initialization))
873891
Void initializations are an advanced technique and should only be used
874892
when profiling indicates that it matters.
875893
)
876-
$(P to void initialise the elements of dynamic array use
894+
$(P To void initialise the *elements* of a dynamic array use
877895
$(REF uninitializedArray, std,array).
878896
)
879897

880898

881899
$(H3 $(LNAME2 static-init-static, Static Initialization of Statically Allocated Arrays))
882900

883901
$(P Static initalizations are supplied by a list of array
884-
element values enclosed in [ ]. The values can be optionally
885-
preceded by an index and a :.
902+
element values enclosed in `[ ]`. The values can be optionally
903+
preceded by an index and a `:`.
886904
If an index is not supplied, it is set to the previous index
887905
plus 1, or 0 if it is the first value.
888906
)
889907

890-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
908+
$(SPEC_RUNNABLE_EXAMPLE_RUN
891909
---------
892910
int[3] a = [ 1:2, 3 ]; // a[0] = 0, a[1] = 2, a[2] = 3
911+
912+
assert(a == [0, 2, 3]);
893913
---------
894914
)
895915

896-
$(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):)
897917

898-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
918+
$(SPEC_RUNNABLE_EXAMPLE_RUN
899919
---------
900920
enum Color { red, blue, green }
901921

902922
int[Color.max + 1] value =
903923
[ Color.blue :6,
904924
Color.green:2,
905925
Color.red :5 ];
926+
927+
assert(value == [5, 6, 2]);
906928
---------
907929
)
908930

0 commit comments

Comments
 (0)