@@ -121,38 +121,75 @@ int[]* e; // pointer to dynamic array of ints
121
121
---------
122
122
)
123
123
124
- $(H2 $(LNAME2 usage , Array Usage ))
124
+ $(H2 $(LNAME2 literals , Array Literals ))
125
125
126
- $(P There are two broad kinds of operations to do on an array -
127
- affecting
128
- the handle to the array,
129
- and affecting the contents of the array.
130
- )
126
+ ---
127
+ auto a1 = [1,2,3]; // type is int[], with elements 1, 2 and 3
128
+ auto a2 = [1u,2,3]; // type is uint[], with elements 1u, 2u, and 3u
129
+ int[2] = [1, 2];
130
+ ---
131
+ $(P `[]` is an empty array literal.)
131
132
132
- $(P The handle to an array is specified by naming the array, as
133
- in p, s or a:
133
+ $(P See $(DDSUBLINK expression, array_literals, Array Literals).)
134
+
135
+ $(LEGACY_LNAME2 usage)
136
+ $(H2 $(LNAME2 assignment, Array Assignment))
137
+
138
+ $(P There are two broad kinds of operations to do on dynamic arrays and
139
+ pointer arrays - those affecting the handle to the array,
140
+ and those affecting the contents of the array. Assignment only affects
141
+ the handle for these types.
134
142
)
135
143
136
- $(SPEC_RUNNABLE_EXAMPLE_FAIL
144
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
137
145
---------
138
146
int* p;
139
147
int[3] s;
140
148
int[] a;
141
149
142
- int[] b;
143
-
144
150
p = s.ptr; // p points to the first element of the array s.
145
151
p = a.ptr; // p points to the first element of the array a.
146
152
147
- a = p; // error, since the length of the array pointed
148
- // to by p is unknown
149
- a = s; // a is initialized to point to the s array
153
+ // error, since the length of the array pointed to by p is unknown
154
+ //s = p;
155
+ //s = a; // error, as a's length is not known at compile-time
156
+ s = [1,2,3];
157
+
158
+ //a = p; // error, length unknown
159
+ a = s; // a points to the elements of s
160
+ assert(a.ptr == s.ptr);
161
+ assert(a == [1,2,3]);
162
+
163
+ int[] b;
150
164
a = b; // a points to the same array as b does
165
+ assert(a.ptr == b.ptr);
166
+ assert(a == []);
151
167
---------
152
168
)
169
+ $(P Each of the three error lines above can be made to copy elements
170
+ using $(RELATIVE_LINK2 slicing, slicing), so that the number of elements
171
+ to copy is then known.)
153
172
154
173
$(H2 $(LNAME2 indexing, Indexing))
155
174
175
+ $(P Indexing allows access to an element of an array:)
176
+
177
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
178
+ ---------
179
+ auto a = [1,2,3];
180
+ assert(a[0] == 1);
181
+ assert(a[2] == 3);
182
+ a[2] = 4;
183
+ assert(a[2] == 4);
184
+ assert(a == [1,2,4]);
185
+ //writeln(a[3]); // runtime error (unless bounds checks turned off)
186
+
187
+ int[2] b = [1,2];
188
+ assert(b[1] == 2);
189
+ //writeln(b[2]); // compile-time error, index out of bounds
190
+ ---------
191
+ )
192
+
156
193
$(P See also $(GLINK2 expression, IndexExpression).)
157
194
158
195
$(H2 $(LNAME2 slicing, Slicing))
@@ -181,7 +218,7 @@ foo(b[1]); // equivalent to foo(3)
181
218
---------
182
219
)
183
220
184
- $(P The [] is shorthand for a slice of the entire array.
221
+ $(P $(I Identifier) [] is shorthand for a slice of the entire array.
185
222
For example, the assignments to b:
186
223
)
187
224
@@ -255,8 +292,10 @@ $(H3 $(LNAME2 overlapping-copying, Overlapping Copying))
255
292
256
293
$(P Overlapping copies are an error:)
257
294
258
- $(SPEC_RUNNABLE_EXAMPLE_FAIL
295
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
259
296
---------
297
+ int[3] s;
298
+
260
299
s[0..2] = s[1..3]; // error, overlapping copy
261
300
s[1..3] = s[0..2]; // error, overlapping copy
262
301
---------
@@ -410,9 +449,6 @@ $(H2 $(LNAME2 pointer-arithmetic, Pointer Arithmetic))
410
449
411
450
$(SPEC_RUNNABLE_EXAMPLE_FAIL
412
451
---------
413
- int[3] abc; // static array of 3 ints
414
- int[] def = [ 1, 2, 3 ]; // dynamic array of 3 ints
415
-
416
452
void dibb(int* array)
417
453
{
418
454
array[2]; // means same thing as *(array + 2)
@@ -694,9 +730,9 @@ $(H2 $(LNAME2 bounds, Array Bounds Checking))
694
730
695
731
$(P It is an error to index an array with an index that is less than
696
732
0 or greater than or equal to the array length. If an index is
697
- out of bounds, a RangeError exception is
698
- raised if detected at runtime, and an error if detected at compile
699
- time. A program may not rely on array bounds checking happening, for
733
+ out of bounds, an `ArrayIndexError` is thrown
734
+ if detected at runtime, and an error is raised if detected at compile
735
+ time. A program may not rely on array bounds checking happening, for
700
736
example, the following program is incorrect:
701
737
)
702
738
711
747
array[i] = 5;
712
748
}
713
749
}
714
- catch (RangeError )
750
+ catch (ArrayIndexError )
715
751
{
716
752
// terminate loop
717
753
}
0 commit comments