How to disable all implicit conversion of primitive types?

Scala seems to behave like Java when it comes to magically transforming primitives:

val a: Int = 1 val b: Double = 2.3 println(a + b) // 3.3 println(Math.max(a, b)) // 2.3 

Most often it was a source of errors in my code. Is there a way to disable these implicit conversions so that my example leads to a warning / error compilation? I would love to write

 print(a.toDouble + b) println(Math.max(a.toDouble, b)) 

every time I need such a transformation.

+6
source share
1 answer

Use a WartRemover . A similar wart is not built-in, but can be written (see "Writing Wart Rules" in README). Although now, I think, this is probably more work than I originally thought.

scalac also has the -Ywarn-numeric-widen (along with -Xfatal-warnings to turn warnings into errors), but I don't know if there are any cases that are not covered by it.

+5
source

Source: https://habr.com/ru/post/978467/


All Articles