I think the biggest problem here is, that you seem to confuse casting and conversion. You have a Map[String, String]
and therefore you can't cast the values to Int
. You have to convert them. Luckily Scala adds the toInt
method to strings through implicit conversion to StringOps.
This should work for you:
m("offset").toInt
Note that toInt
will throw a java.lang.NumberFormatException
if the string can not be converted to an integer.
edit:
What you want will afaik only work with typeclasses.
Here is an example:
trait StringConverter[A] {
def convert(x: String): A
}
implicit object StringToInt extends StringConverter[Int] {
def convert(x: String): Int = x.toInt
}
implicit object StringToDouble extends StringConverter[Double] {
def convert(x: String): Double = x.toDouble
}
implicit def string2StringConversion(x: String) = new {
def toT[A](implicit ev: StringConverter[A]) = ev.convert(x)
}
usage:
scala> "0.".toT[Double]
res6: Double = 0.0