@@ -107,16 +107,22 @@ int[]* e; // pointer to dynamic array of ints
107
107
---------
108
108
)
109
109
110
- $(H2 $(LNAME2 literals, Array Literals))
110
+ $(H2 $(LNAME2 literals, Array Initializers & Literals))
111
111
112
+ $(P An $(GLINK ArrayInitializer) initializes a dynamic or static array:)
112
113
---
113
114
auto a1 = [1,2,3]; // type is int[], with elements 1, 2, and 3
114
115
auto a2 = [1u,2,3]; // type is uint[], with elements 1u, 2u, and 3u
115
116
int[2] a3 = [1,2]; // type is int[2], with elements 1, and 2
116
117
---
117
- $(P `[]` is an empty array literal.)
118
118
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
+ ---
120
126
121
127
$(LEGACY_LNAME2 usage)
122
128
$(H2 $(LNAME2 assignment, Array Assignment))
@@ -1010,14 +1016,29 @@ $(H3 $(LNAME2 void-initialization, Void Initialization))
1010
1016
$(REF uninitializedArray, std,array).
1011
1017
)
1012
1018
1019
+ $(H3 $(LNAME2 array-initializers, Array Initializers))
1013
1020
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)
1015
1029
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
1017
1036
element values enclosed in `[ ]`. The values can be optionally
1018
1037
preceded by an index and a `:`.
1019
1038
If an index is not supplied, it is set to the previous index
1020
1039
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.
1021
1042
)
1022
1043
1023
1044
$(SPEC_RUNNABLE_EXAMPLE_RUN
@@ -1043,6 +1064,26 @@ assert(value == [5, 6, 2]);
1043
1064
---------
1044
1065
)
1045
1066
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
+
1046
1087
$(P All elements of a static array can be initialized to a specific value with:)
1047
1088
1048
1089
$(SPEC_RUNNABLE_EXAMPLE_RUN
@@ -1287,7 +1328,7 @@ $(H3 $(LNAME2 void_arrays, Void Arrays))
1287
1328
the exact type of the array elements are unimportant. The $(D .length) of a
1288
1329
void array is the length of the data in bytes, rather than the number of
1289
1330
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. )
1291
1332
1292
1333
$(P Arrays of any type can be implicitly converted to a (tail qualified) void array - the
1293
1334
compiler inserts the appropriate calculations so that the $(D .length) of
@@ -1310,10 +1351,12 @@ void main()
1310
1351
arr[0..4] = [5]; // Assign first 4 bytes to 1 int element
1311
1352
assert(data1 == [5,2,3]);
1312
1353
1354
+ arr ~= [6]; // Append the 4 bytes of an int
1313
1355
//data1 = arr; // Error: void[] does not implicitly
1314
1356
// convert to int[].
1315
1357
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]);
1317
1360
}
1318
1361
---------
1319
1362
)
0 commit comments