Skip to content

Commit 75aae54

Browse files
authored
Merge pull request #3864 from ntrel/array-init
[spec] Restore *ArrayInitializer* Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents 868574a + 85788a2 commit 75aae54

File tree

3 files changed

+71
-46
lines changed

3 files changed

+71
-46
lines changed

spec/arrays.dd

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,22 @@ int[]* e; // pointer to dynamic array of ints
107107
---------
108108
)
109109

110-
$(H2 $(LNAME2 literals, Array Literals))
110+
$(H2 $(LNAME2 literals, Array Initializers & Literals))
111111

112+
$(P An $(GLINK ArrayInitializer) initializes a dynamic or static array:)
112113
---
113114
auto a1 = [1,2,3]; // type is int[], with elements 1, 2, and 3
114115
auto a2 = [1u,2,3]; // type is uint[], with elements 1u, 2u, and 3u
115116
int[2] a3 = [1,2]; // type is int[2], with elements 1, and 2
116117
---
117-
$(P `[]` is an empty array literal.)
118118

119-
$(P See $(DDSUBLINK spec/expression, array_literals, Array Literals).)
119+
$(P An $(DDSUBLINK spec/expression, array_literals, array literal) is an expression:)
120+
---
121+
void f(int[] a);
122+
123+
f([1, 2]); // pass 2 elements
124+
f([]); // pass an empty array
125+
---
120126

121127
$(LEGACY_LNAME2 usage)
122128
$(H2 $(LNAME2 assignment, Array Assignment))
@@ -1010,14 +1016,29 @@ $(H3 $(LNAME2 void-initialization, Void Initialization))
10101016
$(REF uninitializedArray, std,array).
10111017
)
10121018

1019+
$(H3 $(LNAME2 array-initializers, Array Initializers))
10131020

1014-
$(H3 $(LNAME2 static-init-static, Static Initialization of Statically Allocated Arrays))
1021+
$(GRAMMAR
1022+
$(GNAME ArrayInitializer):
1023+
$(D [) $(I ArrayElementInitializers)$(OPT) $(D ])
1024+
1025+
$(GNAME ArrayElementInitializers):
1026+
$(I ArrayElementInitializer)
1027+
$(I ArrayElementInitializer) $(D ,)
1028+
$(I ArrayElementInitializer) $(D ,) $(GSELF ArrayElementInitializers)
10151029

1016-
$(P Static initalizations are supplied by a list of array
1030+
$(GNAME ArrayElementInitializer):
1031+
$(GLINK2 declaration, NonVoidInitializer)
1032+
$(GLINK2 expression, AssignExpression) $(D :) $(GLINK2 declaration, NonVoidInitializer)
1033+
)
1034+
1035+
$(P An *ArrayInitializer* is a list of array
10171036
element values enclosed in `[ ]`. The values can be optionally
10181037
preceded by an index and a `:`.
10191038
If an index is not supplied, it is set to the previous index
10201039
plus 1, or 0 if it is the first value.
1040+
Any missing elements will be initialized to the default value
1041+
of the element type.
10211042
)
10221043

10231044
$(SPEC_RUNNABLE_EXAMPLE_RUN
@@ -1043,6 +1064,26 @@ assert(value == [5, 6, 2]);
10431064
---------
10441065
)
10451066

1067+
$(P Any indices must be known at compile-time.
1068+
Note that if the array type is not specified and every element has an index,
1069+
it will be inferred as an
1070+
$(DDSUBLINK spec/expression, associative_array_literals, associative array
1071+
literal).)
1072+
1073+
$(SPEC_RUNNABLE_EXAMPLE_RUN
1074+
---
1075+
int n = 4;
1076+
auto aa = [0:1, 3:n]; // associative array `int[int]`
1077+
1078+
int[] a = [1, 3:n, 5];
1079+
assert(a == [1, 0, 0, n, 5]);
1080+
1081+
//int[] e = [n:2]; // error, n not known at compile-time
1082+
---
1083+
)
1084+
1085+
$(H3 $(LNAME2 static-init-static, Static Initialization of Statically Allocated Arrays))
1086+
10461087
$(P All elements of a static array can be initialized to a specific value with:)
10471088

10481089
$(SPEC_RUNNABLE_EXAMPLE_RUN
@@ -1287,7 +1328,7 @@ $(H3 $(LNAME2 void_arrays, Void Arrays))
12871328
the exact type of the array elements are unimportant. The $(D .length) of a
12881329
void array is the length of the data in bytes, rather than the number of
12891330
elements in its original type. Array indices in slicing
1290-
operations are interpreted as byte indices.)
1331+
operations are interpreted as byte indices. A void array cannot be indexed.)
12911332

12921333
$(P Arrays of any type can be implicitly converted to a (tail qualified) void array - the
12931334
compiler inserts the appropriate calculations so that the $(D .length) of
@@ -1310,10 +1351,12 @@ void main()
13101351
arr[0..4] = [5]; // Assign first 4 bytes to 1 int element
13111352
assert(data1 == [5,2,3]);
13121353

1354+
arr ~= [6]; // Append the 4 bytes of an int
13131355
//data1 = arr; // Error: void[] does not implicitly
13141356
// convert to int[].
13151357
int[] data2 = cast(int[]) arr; // OK, can convert with explicit cast.
1316-
assert(data2 is data1);
1358+
assert(data2 is arr); // both point to the same set of bytes
1359+
assert(data2 == [5,2,3,6]);
13171360
}
13181361
---------
13191362
)

spec/declaration.dd

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,38 @@ int x, *y; // x is an int, y is a pointer to int
183183
int x[], y; // x is an array/pointer, y is an int
184184
)
185185

186-
$(H2 $(LEGACY_LNAME2 initializers, initialization, Initialization))
186+
$(H2 $(LNAME2 initialization, Initialization))
187+
188+
$(P When no *Initializer* is given, a variable is set to the
189+
$(DDSUBLINK spec/property, init, default `.init` value) for
190+
its type.)
191+
192+
$(H3 $(LNAME2 initializers, Initializers))
187193

188194
$(GRAMMAR
189195
$(GNAME Initializer):
190196
$(GLINK VoidInitializer)
191197
$(GLINK NonVoidInitializer)
192198

193199
$(GNAME NonVoidInitializer):
194-
$(GLINK2 expression, AssignExpression)$(LEGACY_LNAME2 ExpInitializer)
195-
$(GLINK2 expression, ArrayLiteral)$(LEGACY_LNAME2 ArrayInitializer)
200+
$(GLINK2 arrays, ArrayInitializer)$(LEGACY_LNAME2 ArrayInitializer)
196201
$(GLINK2 struct, StructInitializer)$(LEGACY_LNAME2 StructInitializer)
202+
$(GLINK2 expression, AssignExpression)$(LEGACY_LNAME2 ExpInitializer)
197203
)
198204

199-
$(P When no *Initializer* is given, a variable is set to the
200-
$(DDSUBLINK spec/property, init, default `.init` value) for
201-
its type.)
205+
$(P A variable can be initialized with a $(I NonVoidInitializer).
206+
An *ArrayInitializer* or *StructInitializer* takes precedence over
207+
an expression initializer.)
202208

203-
$(P A variable can be initialized with a $(I NonVoidInitializer).)
209+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
210+
---
211+
struct S { int i; }
204212

205-
$(P See also: $(DDSUBLINK spec/arrays, array-initialization, Array Initialization).)
213+
S s = {}; // struct initializer, not a function literal expression
214+
S[] a = [{2}]; // array initializer holding a struct initializer
215+
//a = [{2}]; // invalid array literal expression
216+
---
217+
)
206218

207219
$(H3 $(LNAME2 void_init, Void Initialization))
208220

spec/expression.dd

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,16 +2101,7 @@ $(H3 $(LNAME2 array_literals, Array Literals))
21012101

21022102
$(GRAMMAR
21032103
$(GNAME ArrayLiteral):
2104-
$(D [) $(I ArrayMemberInitializations)$(OPT) $(D ])
2105-
2106-
$(GNAME ArrayMemberInitializations):
2107-
$(I ArrayMemberInitialization)
2108-
$(I ArrayMemberInitialization) $(D ,)
2109-
$(I ArrayMemberInitialization) $(D ,) $(GSELF ArrayMemberInitializations)
2110-
2111-
$(GNAME ArrayMemberInitialization):
2112-
$(GLINK2 declaration, NonVoidInitializer)
2113-
$(GLINK AssignExpression) $(D :) $(GLINK2 declaration, NonVoidInitializer)
2104+
$(D [) $(GLINK ArgumentList)$(OPT) $(D ])
21142105
)
21152106

21162107
$(P An array literal is a comma-separated list of expressions
@@ -2162,27 +2153,6 @@ $(GNAME ArrayMemberInitialization):
21622153
}
21632154
---
21642155

2165-
$(P To initialize an element at a particular index, use the
2166-
*AssignExpression* `:` *NonVoidInitializer* syntax.
2167-
The *AssignExpression* must be known at compile-time.
2168-
Any missing elements will be initialized to the default value
2169-
of the element type.
2170-
Note that if the array type is not specified, the literal will
2171-
be parsed as an
2172-
$(RELATIVE_LINK2 associative_array_literals, associative array).)
2173-
2174-
$(SPEC_RUNNABLE_EXAMPLE_RUN
2175-
---
2176-
int n = 4;
2177-
auto aa = [0:1, 3:n]; // associative array `int[int]`
2178-
2179-
int[] a = [1, 3:n, 5];
2180-
assert(a == [1, 0, 0, n, 5]);
2181-
2182-
//int[] e = [n:2]; // error, n not known at compile-time
2183-
---
2184-
)
2185-
21862156
$(H4 $(LNAME2 cast_array_literal, Casting))
21872157

21882158
$(P When array literals are cast to another array type, each

0 commit comments

Comments
 (0)