question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Numeric widening and weak conformance

See original GitHub issue

Currently 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:closed
  • Created 6 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

8reactions
sjrdcommented, May 17, 2017

Could we simply remove weak conformance from the language altogether? Seems like a thing inherited from Java that few like anyway.

3reactions
lihaoyicommented, May 22, 2017

@odersky what about the following:

object Foo{
  def main(args: Array[String]): Unit = {
    val list = List(1.2, 92233720368547751L, 4)
    val long =           92233720368547751L
    println(list)
    println(list(1))
    println(list(1).getClass)
    println(list(1).toLong)

    println(long)
    println(list(1).toLong == long)
    
  }
}
sbt: [info] Running Foo
List(1.2, 9.2233720368547744E16, 4.0)
9.2233720368547744E16
double
92233720368547744
92233720368547751
false

(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 of Longs and Doubles that I wrote in the source code. But I don’t and so 92233720368547751L 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found