@@ -1690,15 +1690,24 @@ $(H3 $(LNAME2 array_literals, Array Literals))
1690
1690
1691
1691
$(GRAMMAR
1692
1692
$(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)
1694
1703
)
1695
1704
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
1697
1706
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
1702
1711
to that type.)
1703
1712
1704
1713
---
@@ -1707,41 +1716,20 @@ $(GNAME ArrayLiteral):
1707
1716
---
1708
1717
1709
1718
$(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.)
1713
1721
1714
- $(SPEC_RUNNABLE_EXAMPLE_RUN
1715
1722
-------------
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];
1739
1724
-------------
1740
- )
1741
1725
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.
1745
1733
)
1746
1734
1747
1735
$(P Escaping array literals are allocated on the memory managed heap.
@@ -1754,6 +1742,27 @@ $(GNAME ArrayLiteral):
1754
1742
}
1755
1743
---
1756
1744
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
+
1757
1766
$(H3 $(LNAME2 cast_array_literal, Casting))
1758
1767
1759
1768
$(P When array literals are cast to another array type, each
0 commit comments