Scala seems to behave like Java when it comes to the magic conversion of primitives:
val a: Int = 1
val b: Double = 2.3
println(a + b) // 3.3
println(Math.max(a, b)) // 2.3
More often than not, this has been a source of bugs in my code. Is there a way to disable these implicit conversions so that my example give a compilation warnning/error? I would really rather have to write
print(a.toDouble + b)
println(Math.max(a.toDouble, b))
every single time I need such conversions.
Use WartRemover. A wart like that isn't built-in, but could be written (see "Writing Wart Rules" in README). Though now that I think, it's probably more work than I thought initially.
scalac
also has -Ywarn-numeric-widen
option (together with -Xfatal-warnings
to turn the warnings to errors), but I don't know if there are any cases not covered by it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments