@@ -170,6 +170,9 @@ $(H2 $(LNAME2 assignment, Array Assignment))
170
170
the handle for these types.
171
171
)
172
172
173
+ $(P The `.ptr` property for static and dynamic arrays will give the address
174
+ of the first element in the array:)
175
+
173
176
$(SPEC_RUNNABLE_EXAMPLE_RUN
174
177
---------
175
178
int* p;
@@ -181,8 +184,11 @@ p = a.ptr; // p points to the first element of the array a.
181
184
182
185
// error, since the length of the array pointed to by p is unknown
183
186
//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]);
185
190
s = [1,2,3];
191
+ //s = [1,2,3,4]; // error, too many elements
186
192
187
193
//a = p; // error, length unknown
188
194
a = s; // a points to the elements of s
@@ -195,10 +201,13 @@ assert(a.ptr == b.ptr);
195
201
assert(a == []);
196
202
---------
197
203
)
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
200
206
to copy is then known.)
201
207
208
+ $(P See also $(RELATIVE_LINK2 array-copying, Copying).)
209
+
210
+
202
211
$(H2 $(LNAME2 indexing, Indexing))
203
212
204
213
$(P Indexing allows access to an element of an array:)
@@ -248,7 +257,7 @@ $(H2 $(LNAME2 slicing, Slicing))
248
257
249
258
$(P $(I Slicing) an array means to specify a subarray of it.
250
259
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.
252
261
For example:
253
262
)
254
263
@@ -390,19 +399,28 @@ $(H2 $(LNAME2 array-setting, Array Setting))
390
399
left-hand side are set to the right-hand side.
391
400
)
392
401
393
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
402
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
394
403
---------
395
404
int[3] s;
405
+ int[] a;
396
406
int* p;
397
407
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]);
400
418
---------
401
419
)
402
420
403
421
$(H2 $(LNAME2 array-concatenation, Array Concatenation))
404
422
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
406
424
to concatenate arrays:
407
425
)
408
426
@@ -416,19 +434,19 @@ assert(b == [1, 2, 3, 4]); // concatenate two arrays
416
434
---
417
435
)
418
436
419
- $(P Many languages overload the + operator for concatenation.
437
+ $(P Many languages overload the `+` operator for concatenation.
420
438
This confusingly leads to a dilemma - does:
421
439
)
422
440
423
441
---------
424
442
"10" + 3 + 4
425
443
---------
426
444
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
428
446
result? It isn't obvious, and the language designers wind up carefully
429
447
writing rules to disambiguate it - rules that get incorrectly
430
448
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
432
450
concatenation.
433
451
)
434
452
@@ -452,7 +470,7 @@ assert(a == b);
452
470
453
471
$(H2 $(LNAME2 array-appending, Array Appending))
454
472
455
- $(P Similarly, the ~= operator means append, as in:
473
+ $(P Similarly, the `~=` operator means append, as in:
456
474
)
457
475
458
476
---------
@@ -631,7 +649,7 @@ a.dup; // creates an array of a.length elements, copies
631
649
$(H3 $(LNAME2 resize, Setting Dynamic Array Length))
632
650
633
651
$(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:
635
653
)
636
654
637
655
---------
@@ -854,7 +872,7 @@ $(H3 $(LNAME2 default-initialization, Default Initialization))
854
872
$(H3 $(LNAME2 length-initialization, Length Initialization))
855
873
$(P The $(D new) expression can be used to start a dynamic array
856
874
with a specified length by specifying its type and then using the
857
- () syntax:
875
+ `(size)` syntax:
858
876
)
859
877
860
878
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -873,36 +891,40 @@ $(H3 $(LNAME2 void-initialization, Void Initialization))
873
891
Void initializations are an advanced technique and should only be used
874
892
when profiling indicates that it matters.
875
893
)
876
- $(P to void initialise the elements of dynamic array use
894
+ $(P To void initialise the * elements* of a dynamic array use
877
895
$(REF uninitializedArray, std,array).
878
896
)
879
897
880
898
881
899
$(H3 $(LNAME2 static-init-static, Static Initialization of Statically Allocated Arrays))
882
900
883
901
$(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 `:` .
886
904
If an index is not supplied, it is set to the previous index
887
905
plus 1, or 0 if it is the first value.
888
906
)
889
907
890
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
908
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
891
909
---------
892
910
int[3] a = [ 1:2, 3 ]; // a[0] = 0, a[1] = 2, a[2] = 3
911
+
912
+ assert(a == [0, 2, 3]);
893
913
---------
894
914
)
895
915
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) :)
897
917
898
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
918
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
899
919
---------
900
920
enum Color { red, blue, green }
901
921
902
922
int[Color.max + 1] value =
903
923
[ Color.blue :6,
904
924
Color.green:2,
905
925
Color.red :5 ];
926
+
927
+ assert(value == [5, 6, 2]);
906
928
---------
907
929
)
908
930
0 commit comments