@@ -1991,25 +1991,34 @@ $(GNAME ArrayMemberInitialization):
1991
1991
between square brackets $(D [) and $(D ]).
1992
1992
The expressions form the elements of a dynamic array.
1993
1993
The length of the array is the number of elements.
1994
- The common type of all the elements is taken to be the
1995
- array element type, and each expression is implicitly converted
1996
- to that type.)
1994
+ )
1995
+ $(P
1996
+ The element type of the array is inferred as the common type of all the
1997
+ elements, and each expression is implicitly converted to that type.
1998
+ When there is an expected array type, the elements of the
1999
+ literal will be implicitly converted to the expected element
2000
+ type.)
1997
2001
1998
2002
---
1999
- auto a1 = [1,2,3]; // type is int[], with elements 1, 2 and 3
2000
- auto a2 = [1u,2,3]; // type is uint[], with elements 1u, 2u, and 3u
2003
+ auto a1 = [1, 2, 3]; // type is int[], with elements 1, 2 and 3
2004
+ auto a2 = [1u, 2, 3]; // type is uint[], with elements 1u, 2u, and 3u
2005
+ byte[] a3 = [1, 2, 3]; // OK
2006
+ byte[] a4 = [128]; // error
2001
2007
---
2002
2008
2003
- $(P By default, an array literal is typed as a dynamic array, but the element
2009
+ $(PANEL
2010
+ By default, an array literal is typed as a dynamic array, but the element
2004
2011
count is known at compile time. Therefore, an array literal can be
2005
- implicitly converted to a static array of the same length.)
2012
+ implicitly converted to a static array of the same length.
2006
2013
2007
2014
-------------
2008
- int[2] sa = [1, 2];
2015
+ int[2] sa = [1, 2]; // OK
2016
+ int[2] sb = [1]; // error
2009
2017
-------------
2010
2018
2011
2019
$(NOTE Slicing a dynamic array with a statically known slice length also
2012
2020
$(RELATIVE_LINK2 slice_to_static_array, allows conversion) to a static array.)
2021
+ )
2013
2022
2014
2023
$(P If any $(I ArrayMemberInitialization) is a
2015
2024
$(DDSUBLINK spec/template, TemplateParameterSequence, ValueSeq),
@@ -2056,28 +2065,29 @@ $(H4 $(LNAME2 cast_array_literal, Casting))
2056
2065
reinterpreted as the new type, and the length is recomputed:)
2057
2066
2058
2067
$(SPEC_RUNNABLE_EXAMPLE_RUN
2059
- ---
2060
- import std.stdio;
2061
-
2062
- void main()
2063
- {
2068
+ ---
2064
2069
// cast array literal
2065
- const short [] ct = cast(short []) [cast(byte)1, 1 ];
2070
+ const ubyte [] ct = cast(ubyte []) [257, 257 ];
2066
2071
// this is equivalent to:
2067
- // const short [] ct = [cast(short)1 , cast(short)1 ];
2072
+ // const ubyte [] ct = [cast(ubyte) 257 , cast(ubyte) 257 ];
2068
2073
writeln(ct); // writes [1, 1]
2069
2074
2070
2075
// cast other array expression
2071
2076
// --> normal behavior of CastExpression
2072
- byte[] arr = [cast(byte) 1, cast(byte) 1];
2077
+ byte[] arr = [1, 1];
2073
2078
short[] rt = cast(short[]) arr;
2074
2079
writeln(rt); // writes [257]
2075
- }
2076
- ---
2080
+ ---
2077
2081
)
2078
2082
2079
2083
In other words, casting an array literal will change the type of each initializer element.
2080
2084
2085
+ $(BEST_PRACTICE Avoid casting an array literal when the elements could
2086
+ implicitly convert to an expected type. Instead, declare a variable of that type
2087
+ and initialize it with the array literal.
2088
+ Casting is more bug-prone than implicit conversions.)
2089
+
2090
+
2081
2091
$(H3 $(LNAME2 associative_array_literals, Associative Array Literals))
2082
2092
2083
2093
$(GRAMMAR
0 commit comments