Skip to content

Commit b913f21

Browse files
authored
Merge pull request #3215 from ntrel/arr-lit
Move ArrayInitializer grammar to expression.dd & improve docs Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Dennis <[email protected]>
2 parents df588ad + 7a21e5a commit b913f21

File tree

2 files changed

+49
-50
lines changed

2 files changed

+49
-50
lines changed

spec/declaration.dd

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,12 @@ $(GNAME Initializer):
104104

105105
$(GNAME NonVoidInitializer):
106106
$(GLINK2 expression, AssignExpression)$(LEGACY_LNAME2 ExpInitializer)
107-
$(GLINK ArrayInitializer)
107+
$(GLINK2 expression, ArrayLiteral)$(LEGACY_LNAME2 ArrayInitializer)
108108
$(GLINK2 struct, StructInitializer)$(LEGACY_LNAME2 StructInitializer)
109-
110-
$(GNAME ArrayInitializer):
111-
$(D [) $(GLINK ArrayMemberInitializations)$(OPT) $(D ])
112-
113-
$(GNAME ArrayMemberInitializations):
114-
$(GLINK ArrayMemberInitialization)
115-
$(GLINK ArrayMemberInitialization) $(D ,)
116-
$(GLINK ArrayMemberInitialization) $(D ,) $(GSELF ArrayMemberInitializations)
117-
118-
$(GNAME ArrayMemberInitialization):
119-
$(GLINK NonVoidInitializer)
120-
$(GLINK2 expression, AssignExpression) $(D :) $(GLINK NonVoidInitializer)
121109
)
122110

111+
$(P See also $(GLINK VoidInitializer).)
112+
123113
$(H2 $(LNAME2 declaration_syntax, Declaration Syntax))
124114

125115
$(P Declaration syntax generally reads right to left, including arrays:)

spec/expression.dd

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,15 +1690,24 @@ $(H3 $(LNAME2 array_literals, Array Literals))
16901690

16911691
$(GRAMMAR
16921692
$(GNAME ArrayLiteral):
1693-
$(D [) $(GLINK ArgumentList)$(OPT) $(D ])
1693+
$(D [) $(I ArrayMemberInitializations)$(OPT) $(D ])
1694+
1695+
$(GNAME ArrayMemberInitializations):
1696+
$(I ArrayMemberInitialization)
1697+
$(I ArrayMemberInitialization) $(D ,)
1698+
$(I ArrayMemberInitialization) $(D ,) $(GSELF ArrayMemberInitializations)
1699+
1700+
$(GNAME ArrayMemberInitialization):
1701+
$(GLINK2 declaration, NonVoidInitializer)
1702+
$(GLINK AssignExpression) $(D :) $(GLINK2 declaration, NonVoidInitializer)
16941703
)
16951704

1696-
$(P Array literals are a comma-separated list of $(GLINK AssignExpression)s
1705+
$(P An array literal is a comma-separated list of expressions
16971706
between square brackets $(D [) and $(D ]).
1698-
The $(I AssignExpression)s form the elements of a dynamic array,
1699-
the length of the array is the number of elements.
1700-
The common type of the all elements is taken to be the type of
1701-
the array element, and all elements are implicitly converted
1707+
The expressions form the elements of a dynamic array.
1708+
The length of the array is the number of elements.
1709+
The common type of all the elements is taken to be the
1710+
array element type, and each expression is implicitly converted
17021711
to that type.)
17031712

17041713
---
@@ -1707,41 +1716,20 @@ $(GNAME ArrayLiteral):
17071716
---
17081717

17091718
$(P By default, an array literal is typed as a dynamic array, but the element
1710-
count is known at compile time. So all array literals can be
1711-
implicitly converted to static array types. Slicing a dynamic array with
1712-
statically known bounds also allows conversion to a static array.)
1719+
count is known at compile time. Therefore, an array literal can be
1720+
implicitly converted to a static array of the same length.)
17131721

1714-
$(SPEC_RUNNABLE_EXAMPLE_RUN
17151722
-------------
1716-
void foo(long[2] a)
1717-
{
1718-
assert(a == [2, 3]);
1719-
}
1720-
void bar(ref long[2] a)
1721-
{
1722-
assert(a == [2, 3]);
1723-
a = [4, 5];
1724-
assert(a == [4, 5]);
1725-
}
1726-
1727-
void main()
1728-
{
1729-
long[] arr = [1, 2, 3];
1730-
1731-
foo(arr[1 .. 3]);
1732-
assert(arr == [1, 2, 3]);
1733-
1734-
bar(arr[1 .. 3]);
1735-
assert(arr == [1, 4, 5]);
1736-
1737-
//foo(arr[0 .. 3]); // cannot match length
1738-
}
1723+
int[2] sa = [1, 2];
17391724
-------------
1740-
)
17411725

1742-
$(P If any of the arguments in the $(GLINK ArgumentList) are
1743-
a $(I ValueSeq), then the elements of the $(I ValueSeq)
1744-
are inserted as arguments in place of the sequence.
1726+
$(NOTE Slicing a dynamic array with a statically known slice length also
1727+
$(RELATIVE_LINK2 slice_to_static_array, allows conversion) to a static array.)
1728+
1729+
$(P If any $(I ArrayMemberInitialization) is a
1730+
$(DDSUBLINK spec/template, TemplateParameterSequence, ValueSeq),
1731+
then the elements of the $(I ValueSeq)
1732+
are inserted as expressions in place of the sequence.
17451733
)
17461734

17471735
$(P Escaping array literals are allocated on the memory managed heap.
@@ -1754,6 +1742,27 @@ $(GNAME ArrayLiteral):
17541742
}
17551743
---
17561744

1745+
$(P To initialize an element at a particular index, use the
1746+
*AssignExpression* `:` *NonVoidInitializer* syntax.
1747+
The *AssignExpression* must be known at compile-time.
1748+
Any missing elements will be initialized to the default value
1749+
of the element type.
1750+
Note that if the array type is not specified, the literal will
1751+
be parsed as an
1752+
$(RELATIVE_LINK2 associative_array_literals, associative array).)
1753+
1754+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1755+
---
1756+
int n = 4;
1757+
auto aa = [0:1, 3:n]; // associative array `int[int]`
1758+
1759+
int[] a = [1, 3:n, 5];
1760+
assert(a == [1, 0, 0, n, 5]);
1761+
1762+
//int[] e = [n:2]; // error, n not known at compile-time
1763+
---
1764+
)
1765+
17571766
$(H3 $(LNAME2 cast_array_literal, Casting))
17581767

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

0 commit comments

Comments
 (0)