-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Int convert to Double when unnecessary #1533
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
Comments
This is a result of weak subtyping(see Scala spec section 3.5.3). if(x == 1) x.toString
else ({
if (x == 2) x.toInt
else x.toDouble
}: Double) where the second In case of pattern matching, all 3 versions are joined together as whole and there's no moment when compiler tries to maintain primitives. |
Thank you for explain. |
@XuefengWu. Pattern matching is underspecified in this regard. If you'd count scalac as reference implementation, our behaviour should be considered a bug instead. Spec'ing pattern matching is something that we've been planning to do. For those interested to see the underlying reason in discrepancy between Dotty and scalac:
|
Thanks, I would open an issue for scalac. |
Reopening so that we can discuss whether we would like to actually change this or not, maybe weak subtyping adaptations should not kick in when there's an expected type? @odersky, what do you think of this:
scala> (if (1 == 1) 1 else 2.0): Any
res0: Any = 1
scala> (if (1 == 1) 1 else 2.0): Any
res0: Any = 1.0 |
The original issue is fixed by now with the changes to harmonization. The code
Still gives 1.0, but that's due to constant folding under harmronization. At present I don't feel it's worthwhile to stick a finger into that hornet's nest again. But if someone wants to revisit (including taking responsibility to push this through) feel free to revisit. |
the compiled code box the second result type is Double but not Int.
if use pattern match, it works well in dotty
but this also doest not works well in scalac 2.11/2.12
The text was updated successfully, but these errors were encountered: