Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[spec] Array literal can implicitly convert to an expected type #3701

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Show overflow in example
Fix Issue 16357 - cast(T[])[x] casts x to T instead of [x] to T[].
  • Loading branch information
ntrel committed Oct 6, 2023
commit 4e8db30f0bf3c330c3431e83fe8a75f843b40857
7 changes: 3 additions & 4 deletions spec/expression.dd
Original file line number Diff line number Diff line change
Expand Up @@ -2042,9 +2042,9 @@ $(H4 $(LNAME2 cast_array_literal, Casting))
$(SPEC_RUNNABLE_EXAMPLE_RUN
---
// cast array literal
const short[] ct = cast(short[]) [1, 1];
const ubyte[] ct = cast(ubyte[]) [257, 257];
// this is equivalent to:
// const short[] ct = [cast(short)1, cast(short)1];
// const ubyte[] ct = [cast(ubyte) 257, cast(ubyte) 257];
writeln(ct); // writes [1, 1]

// cast other array expression
Expand All @@ -2059,8 +2059,7 @@ $(H4 $(LNAME2 cast_array_literal, Casting))

$(BEST_PRACTICE Avoid casting an array literal when the elements could
implicitly convert to an expected type. Instead, declare a variable of that type
and initialize it with the array literal. In the example above, the `cast(short[])`
is unnecessary as `1` implicitly converts to short.
and initialize it with the array literal.
Casting is more bug-prone than implicit conversions.)


Expand Down