Skip to content

Commit 52ca9eb

Browse files
authored
Merge pull request #3161 from ntrel/array-tweaks
[spec] Improve array docs Signed-off-by: Max Haughton <maxhaton@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents 53cbbf0 + 2b40383 commit 52ca9eb

File tree

3 files changed

+81
-31
lines changed

3 files changed

+81
-31
lines changed

spec/arrays.dd

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -121,38 +121,75 @@ int[]* e; // pointer to dynamic array of ints
121121
---------
122122
)
123123

124-
$(H2 $(LNAME2 usage, Array Usage))
124+
$(H2 $(LNAME2 literals, Array Literals))
125125

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.)
131132

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.
134142
)
135143

136-
$(SPEC_RUNNABLE_EXAMPLE_FAIL
144+
$(SPEC_RUNNABLE_EXAMPLE_RUN
137145
---------
138146
int* p;
139147
int[3] s;
140148
int[] a;
141149

142-
int[] b;
143-
144150
p = s.ptr; // p points to the first element of the array s.
145151
p = a.ptr; // p points to the first element of the array a.
146152

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;
150164
a = b; // a points to the same array as b does
165+
assert(a.ptr == b.ptr);
166+
assert(a == []);
151167
---------
152168
)
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.)
153172

154173
$(H2 $(LNAME2 indexing, Indexing))
155174

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+
156193
$(P See also $(GLINK2 expression, IndexExpression).)
157194

158195
$(H2 $(LNAME2 slicing, Slicing))
@@ -181,7 +218,7 @@ foo(b[1]); // equivalent to foo(3)
181218
---------
182219
)
183220

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.
185222
For example, the assignments to b:
186223
)
187224

@@ -255,8 +292,10 @@ $(H3 $(LNAME2 overlapping-copying, Overlapping Copying))
255292

256293
$(P Overlapping copies are an error:)
257294

258-
$(SPEC_RUNNABLE_EXAMPLE_FAIL
295+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
259296
---------
297+
int[3] s;
298+
260299
s[0..2] = s[1..3]; // error, overlapping copy
261300
s[1..3] = s[0..2]; // error, overlapping copy
262301
---------
@@ -410,9 +449,6 @@ $(H2 $(LNAME2 pointer-arithmetic, Pointer Arithmetic))
410449

411450
$(SPEC_RUNNABLE_EXAMPLE_FAIL
412451
---------
413-
int[3] abc; // static array of 3 ints
414-
int[] def = [ 1, 2, 3 ]; // dynamic array of 3 ints
415-
416452
void dibb(int* array)
417453
{
418454
array[2]; // means same thing as *(array + 2)
@@ -694,9 +730,9 @@ $(H2 $(LNAME2 bounds, Array Bounds Checking))
694730

695731
$(P It is an error to index an array with an index that is less than
696732
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
700736
example, the following program is incorrect:
701737
)
702738

@@ -711,7 +747,7 @@ try
711747
array[i] = 5;
712748
}
713749
}
714-
catch (RangeError)
750+
catch (ArrayIndexError)
715751
{
716752
// terminate loop
717753
}

spec/expression.dd

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,8 +1577,10 @@ $(GNAME ArrayLiteral):
15771577

15781578
$(P By default, an array literal is typed as a dynamic array, but the element
15791579
count is known at compile time. So all array literals can be
1580-
implicitly converted to static array types.)
1580+
implicitly converted to static array types. Slicing a dynamic array with
1581+
statically known bounds also allows conversion to a static array.)
15811582

1583+
$(SPEC_RUNNABLE_EXAMPLE_RUN
15821584
-------------
15831585
void foo(long[2] a)
15841586
{
@@ -1587,11 +1589,9 @@ $(GNAME ArrayLiteral):
15871589
void bar(ref long[2] a)
15881590
{
15891591
assert(a == [2, 3]);
1590-
a[0] = 4;
1591-
a[1] = 5;
1592+
a = [4, 5];
15921593
assert(a == [4, 5]);
15931594
}
1594-
void baz(const char[3] a) {}
15951595

15961596
void main()
15971597
{
@@ -1603,16 +1603,17 @@ $(GNAME ArrayLiteral):
16031603
bar(arr[1 .. 3]);
16041604
assert(arr == [1, 4, 5]);
16051605

1606-
//baz(arr[1 .. 3]); // cannot match length
1606+
//foo(arr[0 .. 3]); // cannot match length
16071607
}
16081608
-------------
1609+
)
16091610

16101611
$(P If any of the arguments in the $(GLINK ArgumentList) are
16111612
a $(I ValueSeq), then the elements of the $(I ValueSeq)
16121613
are inserted as arguments in place of the sequence.
16131614
)
16141615

1615-
$(P Array literals are allocated on the memory managed heap.
1616+
$(P Escaping array literals are allocated on the memory managed heap.
16161617
Thus, they can be returned safely from functions:)
16171618

16181619
---
@@ -1646,7 +1647,7 @@ $(GNAME ArrayLiteral):
16461647
}
16471648
---
16481649

1649-
In other words, casting literal expression will change the literal type.
1650+
In other words, casting an array literal will change the type of each initializer element.
16501651

16511652
$(H3 $(LNAME2 associative_array_literals, Associative Array Literals))
16521653

@@ -1682,7 +1683,7 @@ $(GNAME ValueExpression):
16821683
anything.)
16831684

16841685
---
1685-
[21u:"he", 38:"ho", 2:"hi"]; // type is string[uint],
1686+
[21u: "he", 38: "ho", 2: "hi"]; // type is string[uint],
16861687
// with keys 21u, 38u and 2u
16871688
// and values "he", "ho", and "hi"
16881689
---

spec/hash-map.dd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ $(HEADERNAV_TOC)
1212
within the $(D [ ]) of an array declaration:
1313
)
1414

15+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1516
---------
1617
int[string] aa; // Associative array of ints that are
1718
// indexed by string keys.
@@ -20,6 +21,7 @@ $(HEADERNAV_TOC)
2021
int value = aa["hello"]; // lookup value from a key
2122
assert(value == 3);
2223
---------
24+
)
2325

2426
$(P Neither the $(I KeyType)s nor the element types of an associative
2527
array can be function types or $(D void).
@@ -29,6 +31,17 @@ $(HEADERNAV_TOC)
2931
of the keys inserted into the array. In particular, in a $(D foreach) loop the
3032
order in which the elements are iterated is typically unspecified.)
3133

34+
$(H2 $(LNAME2 literals, Literals))
35+
36+
$(SPEC_RUNNABLE_EXAMPLE_RUN
37+
---
38+
auto aa = [21u: "he", 38: "ho", 2: "hi"];
39+
static assert(is(typeof(aa) == string[uint]));
40+
assert(aa[2] == "hi");
41+
---
42+
)
43+
$(P See $(DDSUBLINK expression, associative_array_literals, Associative Array Literals).)
44+
3245
$(H2 $(LNAME2 removing_keys, Removing Keys))
3346

3447
$(P Particular keys in an associative array can be removed with the

0 commit comments

Comments
 (0)