rrr rrr - 5 months ago 52
Scala Question

Casting error from Option[Double] to Double in scala

I am facing problem in casting Option[Double] to Double. I am getting following exception error

scala.Some cannot be cast to java.lang.Double

val ty=ttnew.distinct().leftOuterJoin(rank).map{ case ((key),(c,d)) => (key,c._1,c._2,c._3,d.getOrElse(Double).asInstanceOf[Double]) }

Please Help!


makes no sense.

Assuming d is an Option[Double] if you want to have a Double you cannot cast it, you need to get the value "out of" the Option.

getOrElse allows you to get the value if present, and provide a fallback value if absent (i.e. None).

For example:

d.getOrElse(0) // 0, or anything that makes sense as a fallback

If d has type Option[Double] then d.getOrElse(0) has type Double.