@@ -127,6 +127,9 @@ $(H2 $(LNAME2 assignment, Array Assignment))
127
127
the handle for these types.
128
128
)
129
129
130
+ $(P The `.ptr` property for static and dynamic arrays will give the address
131
+ of the first element in the array:)
132
+
130
133
$(SPEC_RUNNABLE_EXAMPLE_RUN
131
134
---------
132
135
int* p;
@@ -138,24 +141,49 @@ p = a.ptr; // p points to the first element of the array a.
138
141
139
142
// error, since the length of the array pointed to by p is unknown
140
143
//s = p;
141
- //s = a; // error, as a's length is not known at compile-time
142
- s = [1,2,3];
143
144
144
145
//a = p; // error, length unknown
145
146
a = s; // a points to the elements of s
146
147
assert(a.ptr == s.ptr);
147
- assert(a == [1,2,3]);
148
148
149
149
int[] b;
150
150
a = b; // a points to the same array as b does
151
151
assert(a.ptr == b.ptr);
152
152
assert(a == []);
153
153
---------
154
154
)
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
157
157
to copy is then known.)
158
158
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
+
159
187
$(H2 $(LNAME2 indexing, Indexing))
160
188
161
189
$(P Indexing allows access to an element of an array:)
@@ -205,7 +233,7 @@ $(H2 $(LNAME2 slicing, Slicing))
205
233
206
234
$(P $(I Slicing) an array means to specify a subarray of it.
207
235
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.
209
237
For example:
210
238
)
211
239
@@ -291,18 +319,26 @@ $(H2 $(LNAME2 array-copying, Array Copying))
291
319
right-hand side is an array of or pointer to the same type.
292
320
)
293
321
294
- $(SPEC_RUNNABLE_EXAMPLE_FAIL
322
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
295
323
---------
296
- int[3] s;
297
- int[3] t ;
324
+ int[3] s, t ;
325
+ int[] a ;
298
326
299
327
s = t; // the 3 elements of t are copied into s
300
328
s[] = t; // the 3 elements of t are copied into s
301
329
s[] = t[]; // the 3 elements of t are copied into s
302
330
s[1..2] = t[0..1]; // same as s[1] = t[0]
303
331
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]);
306
342
---------
307
343
)
308
344
@@ -347,19 +383,28 @@ $(H2 $(LNAME2 array-setting, Array Setting))
347
383
left-hand side are set to the right-hand side.
348
384
)
349
385
350
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
386
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
351
387
---------
352
388
int[3] s;
389
+ int[] a;
353
390
int* p;
354
391
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]);
357
402
---------
358
403
)
359
404
360
405
$(H2 $(LNAME2 array-concatenation, Array Concatenation))
361
406
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
363
408
to concatenate arrays:
364
409
)
365
410
@@ -373,19 +418,19 @@ assert(b == [1, 2, 3, 4]); // concatenate two arrays
373
418
---
374
419
)
375
420
376
- $(P Many languages overload the + operator for concatenation.
421
+ $(P Many languages overload the `+` operator for concatenation.
377
422
This confusingly leads to a dilemma - does:
378
423
)
379
424
380
425
---------
381
426
"10" + 3 + 4
382
427
---------
383
428
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
385
430
result? It isn't obvious, and the language designers wind up carefully
386
431
writing rules to disambiguate it - rules that get incorrectly
387
432
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
389
434
concatenation.
390
435
)
391
436
@@ -409,7 +454,7 @@ assert(a == b);
409
454
410
455
$(H2 $(LNAME2 array-appending, Array Appending))
411
456
412
- $(P Similarly, the ~= operator means append, as in:
457
+ $(P Similarly, the `~=` operator means append, as in:
413
458
)
414
459
415
460
---------
@@ -588,7 +633,7 @@ a.dup; // creates an array of a.length elements, copies
588
633
$(H3 $(LNAME2 resize, Setting Dynamic Array Length))
589
634
590
635
$(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:
592
637
)
593
638
594
639
---------
@@ -827,7 +872,7 @@ $(H3 $(LNAME2 default-initialization, Default Initialization))
827
872
$(H3 $(LNAME2 length-initialization, Length Initialization))
828
873
$(P The $(D new) expression can be used to start a dynamic array
829
874
with a specified length by specifying its type and then using the
830
- () syntax:
875
+ `(size)` syntax:
831
876
)
832
877
833
878
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -846,36 +891,40 @@ $(H3 $(LNAME2 void-initialization, Void Initialization))
846
891
Void initializations are an advanced technique and should only be used
847
892
when profiling indicates that it matters.
848
893
)
849
- $(P to void initialise the elements of dynamic array use
894
+ $(P To void initialise the * elements* of a dynamic array use
850
895
$(REF uninitializedArray, std,array).
851
896
)
852
897
853
898
854
899
$(H3 $(LNAME2 static-init-static, Static Initialization of Statically Allocated Arrays))
855
900
856
901
$(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 `:` .
859
904
If an index is not supplied, it is set to the previous index
860
905
plus 1, or 0 if it is the first value.
861
906
)
862
907
863
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
908
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
864
909
---------
865
910
int[3] a = [ 1:2, 3 ]; // a[0] = 0, a[1] = 2, a[2] = 3
911
+
912
+ assert(a == [0, 2, 3]);
866
913
---------
867
914
)
868
915
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) :)
870
917
871
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
918
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
872
919
---------
873
920
enum Color { red, blue, green }
874
921
875
922
int[Color.max + 1] value =
876
923
[ Color.blue :6,
877
924
Color.green:2,
878
925
Color.red :5 ];
926
+
927
+ assert(value == [5, 6, 2]);
879
928
---------
880
929
)
881
930
0 commit comments