I'm trying to instantiate classes from Maps[String, Any], receiving through some json-rpc. So I end up with following problem:
val mpa:Map[String, Any] = Map("key"->0.0)
implicit def anyToInt(a:Any):Int = a.asInstanceOf[Double].toInt
When key exists all is OK.
val i:Int = mpa.getOrElse("key", 0.0)
i: Int = 0
But when key is missing ...:
scala> val i:Int = mpa.getOrElse("val", 0.0)
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
at scala.runtime.BoxesRunTime.unboxToDouble(Unknown Source)
at .anyToInt(<console>:13
Now, if we're add some verbosity as:
implicit def anyToInt(a:Any):Int = {
println(a)
val b = a.asInstanceOf[Double].toInt
println("converted")
b
}
We got:
val i:Int = mpa.getOrElse("val", 0.0)
0.0
converted
0
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
.....
So i came to the conclusion that anyToInt is called twice. And the second time it received Int as Any.
Questions:
Why ?!
What should I do to avoid this ?
P.S.: Sorry if it's newbie question. I'm new in scala.