Skip to content

Commit 285a29b

Browse files
committed
Document index:element initializers
1 parent 126539a commit 285a29b

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

spec/expression.dd

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ $(GNAME ArrayMemberInitialization):
17021702
$(GLINK AssignExpression) $(D :) $(GLINK2 declaration, NonVoidInitializer)
17031703
)
17041704

1705-
$(P Array literals are a comma-separated list of expressions
1705+
$(P An array literal is a comma-separated list of expressions
17061706
between square brackets $(D [) and $(D ]).
17071707
The expressions form the elements of a dynamic array.
17081708
The length of the array is the number of elements.
@@ -1764,6 +1764,27 @@ $(GNAME ArrayMemberInitialization):
17641764
}
17651765
---
17661766

1767+
$(P To initialize an element at a particular index, use the
1768+
*AssignExpression* `:` *NonVoidInitializer* syntax.
1769+
The *AssignExpression* must be known at compile-time.
1770+
Any missing elements will be initialized to the default value
1771+
of the element type.
1772+
Note that if the array type is not specified, the literal will
1773+
be parsed as an
1774+
$(RELATIVE_LINK2 associative_array_literals, associative array).)
1775+
1776+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1777+
---
1778+
int n = 4;
1779+
auto aa = [0:1, 3:n]; // associative array `int[int]`
1780+
1781+
int[] a = [1, 3:n, 5];
1782+
assert(a == [1, 0, 0, n, 5]);
1783+
1784+
//int[] e = [n:2]; // error, n not known at compile-time
1785+
---
1786+
)
1787+
17671788
$(H3 $(LNAME2 cast_array_literal, Casting))
17681789

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

0 commit comments

Comments
 (0)