Skip to content

Commit b04ba82

Browse files
authored
[spec] Array literal can implicitly convert to an expected type (#3701)
* [spec] Array literal can implicitly convert to an expected type Fix Issue 24177 - Array literal can implicitly convert to an expected type. Use this feature to avoid casting elements manually in example. * Avoid casting when implicit conversion can be used * Show overflow in example Fix Issue 16357 - cast(T[])[x] casts x to T instead of [x] to T[].
1 parent 81fbea0 commit b04ba82

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

spec/expression.dd

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,25 +1991,34 @@ $(GNAME ArrayMemberInitialization):
19911991
between square brackets $(D [) and $(D ]).
19921992
The expressions form the elements of a dynamic array.
19931993
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.)
19972001

19982002
---
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
20012007
---
20022008

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
20042011
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.
20062013

20072014
-------------
2008-
int[2] sa = [1, 2];
2015+
int[2] sa = [1, 2]; // OK
2016+
int[2] sb = [1]; // error
20092017
-------------
20102018

20112019
$(NOTE Slicing a dynamic array with a statically known slice length also
20122020
$(RELATIVE_LINK2 slice_to_static_array, allows conversion) to a static array.)
2021+
)
20132022

20142023
$(P If any $(I ArrayMemberInitialization) is a
20152024
$(DDSUBLINK spec/template, TemplateParameterSequence, ValueSeq),
@@ -2056,28 +2065,29 @@ $(H4 $(LNAME2 cast_array_literal, Casting))
20562065
reinterpreted as the new type, and the length is recomputed:)
20572066

20582067
$(SPEC_RUNNABLE_EXAMPLE_RUN
2059-
---
2060-
import std.stdio;
2061-
2062-
void main()
2063-
{
2068+
---
20642069
// cast array literal
2065-
const short[] ct = cast(short[]) [cast(byte)1, 1];
2070+
const ubyte[] ct = cast(ubyte[]) [257, 257];
20662071
// this is equivalent to:
2067-
// const short[] ct = [cast(short)1, cast(short)1];
2072+
// const ubyte[] ct = [cast(ubyte) 257, cast(ubyte) 257];
20682073
writeln(ct); // writes [1, 1]
20692074

20702075
// cast other array expression
20712076
// --> normal behavior of CastExpression
2072-
byte[] arr = [cast(byte)1, cast(byte)1];
2077+
byte[] arr = [1, 1];
20732078
short[] rt = cast(short[]) arr;
20742079
writeln(rt); // writes [257]
2075-
}
2076-
---
2080+
---
20772081
)
20782082

20792083
In other words, casting an array literal will change the type of each initializer element.
20802084

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+
20812091
$(H3 $(LNAME2 associative_array_literals, Associative Array Literals))
20822092

20832093
$(GRAMMAR

0 commit comments

Comments
 (0)