我有以下方法,它返回两点之间的距离的绝对值作为Int
.
def absDist(a: Tuple2[Int, Int], b: Tuple2[Int, Int]): Int = { ((scala.math.pow(a._1 - b._1, 2) + scala.math.pow(a._2 - b._2, 2)): Int) }
但是,类型无法转换:
Expression of type Double doesn't conform to expected type Int
为什么会这样?我的转换对我来说很好看.
用于toInt
进行类型转换:
def absDist(a: Tuple2[Int, Int], b: Tuple2[Int, Int]): Int = { (scala.math.pow(a._1 - b._1, 2) + scala.math.pow(a._2 - b._2, 2)).toInt }