@@ -1032,13 +1032,13 @@ $(GNAME ArrayElementInitializer):
1032
1032
$(GLINK2 expression, AssignExpression) $(D :) $(GLINK2 declaration, NonVoidInitializer)
1033
1033
)
1034
1034
1035
- $(H4 $(LNAME2 static-init-static, Static Initialization of Statically Allocated Arrays))
1036
-
1037
- $(P Static initalizations are supplied by a list of array
1035
+ $(P An *ArrayInitializer* is a list of array
1038
1036
element values enclosed in `[ ]`. The values can be optionally
1039
1037
preceded by an index and a `:`.
1040
1038
If an index is not supplied, it is set to the previous index
1041
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.
1042
1042
)
1043
1043
1044
1044
$(SPEC_RUNNABLE_EXAMPLE_RUN
@@ -1064,29 +1064,9 @@ assert(value == [5, 6, 2]);
1064
1064
---------
1065
1065
)
1066
1066
1067
- $(P All elements of a static array can be initialized to a specific value with:)
1068
-
1069
- $(SPEC_RUNNABLE_EXAMPLE_RUN
1070
- ---------
1071
- int[4] a = 42; // set all elements of a to 42
1072
-
1073
- assert(a == [42, 42, 42, 42]);
1074
- ---------
1075
- )
1076
-
1077
- $(P These arrays are statically allocated when they appear in global scope.
1078
- Otherwise, they need to be marked with $(D const) or $(D static)
1079
- storage classes to make them statically allocated arrays.)
1080
-
1081
- $(H4 $(LNAME2 index-initializers, Index Initializers))
1082
-
1083
- $(P To initialize an element at a particular index, use the
1084
- *AssignExpression* `:` *NonVoidInitializer* syntax.
1085
- The *AssignExpression* must be known at compile-time.
1086
- Any missing elements will be initialized to the default value
1087
- of the element type.
1088
- Note that if the array type is not specified, the array initializer will
1089
- be parsed as an
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
1090
1070
$(DDSUBLINK spec/expression, associative_array_literals, associative array
1091
1071
literal).)
1092
1072
@@ -1102,6 +1082,22 @@ $(H4 $(LNAME2 index-initializers, Index Initializers))
1102
1082
---
1103
1083
)
1104
1084
1085
+ $(H3 $(LNAME2 static-init-static, Static Initialization of Statically Allocated Arrays))
1086
+
1087
+ $(P All elements of a static array can be initialized to a specific value with:)
1088
+
1089
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1090
+ ---------
1091
+ int[4] a = 42; // set all elements of a to 42
1092
+
1093
+ assert(a == [42, 42, 42, 42]);
1094
+ ---------
1095
+ )
1096
+
1097
+ $(P These arrays are statically allocated when they appear in global scope.
1098
+ Otherwise, they need to be marked with $(D const) or $(D static)
1099
+ storage classes to make them statically allocated arrays.)
1100
+
1105
1101
1106
1102
$(H2 $(LNAME2 special-array, Special Array Types))
1107
1103
@@ -1332,7 +1328,7 @@ $(H3 $(LNAME2 void_arrays, Void Arrays))
1332
1328
the exact type of the array elements are unimportant. The $(D .length) of a
1333
1329
void array is the length of the data in bytes, rather than the number of
1334
1330
elements in its original type. Array indices in slicing
1335
- operations are interpreted as byte indices.)
1331
+ operations are interpreted as byte indices. A void array cannot be indexed. )
1336
1332
1337
1333
$(P Arrays of any type can be implicitly converted to a (tail qualified) void array - the
1338
1334
compiler inserts the appropriate calculations so that the $(D .length) of
@@ -1355,10 +1351,12 @@ void main()
1355
1351
arr[0..4] = [5]; // Assign first 4 bytes to 1 int element
1356
1352
assert(data1 == [5,2,3]);
1357
1353
1354
+ arr ~= [6]; // Append the 4 bytes of an int
1358
1355
//data1 = arr; // Error: void[] does not implicitly
1359
1356
// convert to int[].
1360
1357
int[] data2 = cast(int[]) arr; // OK, can convert with explicit cast.
1361
- assert(data2 is data1);
1358
+ assert(data2 is arr); // both point to the same set of bytes
1359
+ assert(data2 == [5,2,3,6]);
1362
1360
}
1363
1361
---------
1364
1362
)
0 commit comments