For example, this code compiles and outputs false, because Scala compiler implicitly converts Ints to Doubles instead of creating List[AnyVal], and this looks strange to me, because this can lead to data loss (floating-point types are not precise, for example; and it can do similar thing with other types that may have dangerous implicit conversions). Why designers of Scala have chosen to do it this way? (I’m using version 2.9)
object Main {
def main(args: Array[String]) {
val x = List(3, 2)
val y = List(3, 2.0)
println(x.head / 2 == y.head / 2)
}
}
You have only two possibilities here: promote to
Doubleor widen toAnyVal. In the latter case, division isn’t even defined, which is kind of useless. If you want to demand this behavior, you can explicitly specify the type: