oct*_*ian 0 casting scala type-conversion
我有以下方法,它返回两点之间的距离的绝对值作为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)
}
Run Code Online (Sandbox Code Playgroud)
但是,类型无法转换:
Expression of type Double doesn't conform to expected type Int
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我的转换对我来说很好看.
用于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
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
615 次 |
最近记录: |