Skip to content

Commit 85788a2

Browse files
committed
Rework index initializers and move static array subheading
1 parent c04b8fd commit 85788a2

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

spec/arrays.dd

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,13 +1032,13 @@ $(GNAME ArrayElementInitializer):
10321032
$(GLINK2 expression, AssignExpression) $(D :) $(GLINK2 declaration, NonVoidInitializer)
10331033
)
10341034

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
10381036
element values enclosed in `[ ]`. The values can be optionally
10391037
preceded by an index and a `:`.
10401038
If an index is not supplied, it is set to the previous index
10411039
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.
10421042
)
10431043

10441044
$(SPEC_RUNNABLE_EXAMPLE_RUN
@@ -1064,29 +1064,9 @@ assert(value == [5, 6, 2]);
10641064
---------
10651065
)
10661066

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
10901070
$(DDSUBLINK spec/expression, associative_array_literals, associative array
10911071
literal).)
10921072

@@ -1102,6 +1082,22 @@ $(H4 $(LNAME2 index-initializers, Index Initializers))
11021082
---
11031083
)
11041084

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+
11051101

11061102
$(H2 $(LNAME2 special-array, Special Array Types))
11071103

@@ -1332,7 +1328,7 @@ $(H3 $(LNAME2 void_arrays, Void Arrays))
13321328
the exact type of the array elements are unimportant. The $(D .length) of a
13331329
void array is the length of the data in bytes, rather than the number of
13341330
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.)
13361332

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

1354+
arr ~= [6]; // Append the 4 bytes of an int
13581355
//data1 = arr; // Error: void[] does not implicitly
13591356
// convert to int[].
13601357
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]);
13621360
}
13631361
---------
13641362
)

0 commit comments

Comments
 (0)