Numeric widening and weak conformance
See original GitHub issueCurrently we’re doing what scalac is doing WRT to this issue. Example:
List(1, 2.3) // res: List[Double]
from a user standpoint, this should in a stricter language have been inferred as List[Int | Double]
. In scalac they have a flag for this Ywarn-numeric-widen
. Which is not yet implemented in Dotty.
If we choose not to make inference for this type of construct stricter, then we should definitely implement the Ywarn-numeric-widen
-flag.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
implicit conversion over multiple levels, why does int to double ...
The one of interest is numeric widening: If e has a primitive number type which weakly conforms to the expected type, it is...
Read more >Dropped: Weak Conformance - Scala 3 - EPFL
Weak conformance applies to all "numeric" types (including Char ), and independently of whether the expressions are literals or not. However, in hindsight,...
Read more >I removed all numeric widening conversions - Google Groups
The partial order is weak subtyping, i.e. subtyping augmented by numeric widening (*). I see the attraction of simplifying the partial order and...
Read more >Proposal to drop Weak Conformance from the language - #101 by ...
If e has a primitive number type which weakly conforms to the expected type, it is widened to the expected type using one...
Read more >Scala – implicit conversion over multiple levels, why does int to ...
The one of interest is numeric widening: ... 3.5.16 Weak Conformance says. In some situations Scala uses a more general conformance relation.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Could we simply remove weak conformance from the language altogether? Seems like a thing inherited from Java that few like anyway.
@odersky what about the following:
(https://scastie.scala-lang.org/btdDoZBcRNqJRWm12VMXWA)
Here, we have the same literal resulting in two different, unequal values, due to widening/conformance/whatever. There are no type annotations here, so I don’t expect any implicits to kick in: if anything, I’d expect to get a
List[Any]
containing the mix ofLong
s andDouble
s that I wrote in the source code. But I don’t and so92233720368547751L
ends up being two different values.Surely the same literal resulting in different values is a real problem, and that’s not just pedantic busywork?
I don’t really care what the solution is, whether it’s union types or intersection types or inferring
Any
or whatever. I just don’t want my numbers mysteriously, irreversibly losing precision despite there being no explicit conversions in sight