Skip to content

Commit fc3580e

Browse files
committed
[spec] Improve array docs
Improve *Array Usage* example and link to *Slicing*. Add *Array Literals* section. Add example for *Indexing*. Remove unnecessary variables in *Pointer Arithmetic* example. [expression.dd] Simplify literal -> static array example. [hash-map.dd] Add AA *Literals* section.
1 parent 1dc8970 commit fc3580e

File tree

3 files changed

+65
-21
lines changed

3 files changed

+65
-21
lines changed

spec/arrays.dd

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,35 +124,66 @@ int[]* e; // pointer to dynamic array of ints
124124
$(H2 $(LNAME2 usage, Array Usage))
125125

126126
$(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.
127+
those affecting the handle to the array,
128+
and those affecting the contents of the array.
130129
)
131130

132131
$(P The handle to an array is specified by naming the array, as
133132
in p, s or a:
134133
)
135134

136-
$(SPEC_RUNNABLE_EXAMPLE_FAIL
135+
$(SPEC_RUNNABLE_EXAMPLE_RUN
137136
---------
138137
int* p;
139138
int[3] s;
140139
int[] a;
141140

142-
int[] b;
143-
144141
p = s.ptr; // p points to the first element of the array s.
145142
p = a.ptr; // p points to the first element of the array a.
146143

147-
a = p; // error, since the length of the array pointed
148-
// to by p is unknown
144+
// error, since the length of the array pointed to by p is unknown
145+
//s = p;
146+
//s = a; // error, as a's length is not known at compile-time
147+
148+
//a = p; // error, length unknown
149149
a = s; // a is initialized to point to the s array
150+
assert(a.ptr == s.ptr);
151+
152+
int[] b;
150153
a = b; // a points to the same array as b does
154+
assert(a.ptr == b.ptr);
151155
---------
152156
)
157+
$(P Each of the three error lines above can be made to copy elements
158+
using $(RELATIVE_LINK2 slicing, slicing), so that the number of elements
159+
to copy is then known.)
160+
161+
$(H2 $(LNAME2 literals, Array Literals))
162+
163+
---
164+
auto a1 = [1,2,3]; // type is int[], with elements 1, 2 and 3
165+
a1 = []; // a1 is now empty
166+
auto a2 = [1u,2,3]; // type is uint[], with elements 1u, 2u, and 3u
167+
int[2] = [1, 2];
168+
---
169+
170+
$(P See $(DDSUBLINK expression, array_literals, Array Literals).)
153171

154172
$(H2 $(LNAME2 indexing, Indexing))
155173

174+
$(P Indexing allows access to an element of an array:)
175+
176+
$(SPEC_RUNNABLE_EXAMPLE_RUN
177+
---------
178+
auto a = [1,2,3];
179+
assert(a[0] == 1);
180+
assert(a[2] == 3);
181+
a[2] = 4;
182+
assert(a[2] == 4);
183+
assert(a == [1,2,4]);
184+
---------
185+
)
186+
156187
$(P See also $(GLINK2 expression, IndexExpression).)
157188

158189
$(H2 $(LNAME2 slicing, Slicing))
@@ -181,7 +212,7 @@ foo(b[1]); // equivalent to foo(3)
181212
---------
182213
)
183214

184-
$(P The [] is shorthand for a slice of the entire array.
215+
$(P $(I Identifier)[] is shorthand for a slice of the entire array.
185216
For example, the assignments to b:
186217
)
187218

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

256287
$(P Overlapping copies are an error:)
257288

258-
$(SPEC_RUNNABLE_EXAMPLE_FAIL
289+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
259290
---------
291+
int[3] s;
292+
260293
s[0..2] = s[1..3]; // error, overlapping copy
261294
s[1..3] = s[0..2]; // error, overlapping copy
262295
---------
@@ -410,9 +443,6 @@ $(H2 $(LNAME2 pointer-arithmetic, Pointer Arithmetic))
410443

411444
$(SPEC_RUNNABLE_EXAMPLE_FAIL
412445
---------
413-
int[3] abc; // static array of 3 ints
414-
int[] def = [ 1, 2, 3 ]; // dynamic array of 3 ints
415-
416446
void dibb(int* array)
417447
{
418448
array[2]; // means same thing as *(array + 2)
@@ -694,7 +724,7 @@ $(H2 $(LNAME2 bounds, Array Bounds Checking))
694724

695725
$(P It is an error to index an array with an index that is less than
696726
0 or greater than or equal to the array length. If an index is
697-
out of bounds, a RangeError exception is
727+
out of bounds, a `RangeError` exception is
698728
raised if detected at runtime, and an error if detected at compile
699729
time. A program may not rely on array bounds checking happening, for
700730
example, the following program is incorrect:

spec/expression.dd

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,8 +1554,10 @@ $(GNAME ArrayLiteral):
15541554

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

1560+
$(SPEC_RUNNABLE_EXAMPLE_RUN
15591561
-------------
15601562
void foo(long[2] a)
15611563
{
@@ -1564,11 +1566,9 @@ $(GNAME ArrayLiteral):
15641566
void bar(ref long[2] a)
15651567
{
15661568
assert(a == [2, 3]);
1567-
a[0] = 4;
1568-
a[1] = 5;
1569+
a = [4, 5];
15691570
assert(a == [4, 5]);
15701571
}
1571-
void baz(const char[3] a) {}
15721572

15731573
void main()
15741574
{
@@ -1580,16 +1580,17 @@ $(GNAME ArrayLiteral):
15801580
bar(arr[1 .. 3]);
15811581
assert(arr == [1, 4, 5]);
15821582

1583-
//baz(arr[1 .. 3]); // cannot match length
1583+
//foo(arr[0..3]); // cannot match length
15841584
}
15851585
-------------
1586+
)
15861587

15871588
$(P If any of the arguments in the $(GLINK ArgumentList) are
15881589
a $(I ValueSeq), then the elements of the $(I ValueSeq)
15891590
are inserted as arguments in place of the sequence.
15901591
)
15911592

1592-
$(P Array literals are allocated on the memory managed heap.
1593+
$(P Escaping array literals are allocated on the memory managed heap.
15931594
Thus, they can be returned safely from functions:)
15941595

15951596
---
@@ -1623,7 +1624,7 @@ $(GNAME ArrayLiteral):
16231624
}
16241625
---
16251626

1626-
In other words, casting literal expression will change the literal type.
1627+
In other words, casting an array literal will change the type of each initializer element.
16271628

16281629
$(H3 $(LNAME2 associative_array_literals, Associative Array Literals))
16291630

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)