bsky
bsky

Reputation: 20222

Scala can not convert expression of Double to Int

I have the following method, which returns the absolute value of the distance between two points as an 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)
  }

However, the type can not be converted:

Expression of type Double doesn't conform to expected type Int

Why is this happening? My conversion looks good to me.

Upvotes: 0

Views: 1743

Answers (1)

Amit
Amit

Reputation: 46323

Use toInt to do type conversion:

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
}

Upvotes: 5

Related Questions