Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable all implicit conversion of primitive types?

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.

like image 581
OlivierBlanvillain Avatar asked Nov 20 '14 15:11

OlivierBlanvillain


1 Answers

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.

like image 160
Alexey Romanov Avatar answered Sep 23 '22 12:09

Alexey Romanov