Union type auto reduction/inference on pattern matches. #12810
Locked
Swoorup
started this conversation in
General Discussion
Replies: 1 comment 1 reply
-
This can be archived, as I created a feature request on dotty |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In typescript I can do like the following:
Here,
x
is deduced to a union of"B" | "C"
.With scala 3 union unfortunately you can't do so at the moment. The following fails to compile:
This will lead people to instead use
asInstanceOf
which is bad imo. You can still make it typesafe if you annotate the last type, without warnings likeBut it would be nice, if scala was able to auto deduce the type of
x
toInt | Boolean
, leading to more natural code and less usage ofasInstanceOf
.If there was a condition on the pattern match, we could retain the current behavior as is.
Beta Was this translation helpful? Give feedback.
All reactions