Using asInstanceOf to convert Any to Double

Viewed 11561

I have a function that takes a variable number of arguments. The first is a String and the rest are numbers (either Int or Double) so I am using Any* to get the arguments. I would like to treat the numbers uniformly as Doubles, but I cannot just use asInstanceOf[Double] on the numeric arguments. For example:

 val arr = Array("varargs list of numbers", 3, 4.2, 5)
 val d = arr(1).asInstanceOf[Double]

gives:

 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

Is there a way to do this? (The function needs to add up all the numbers).

4 Answers
Related