Scala create a numeric from a string

Viewed 110

I want to do something like this:

def parse[A: Numeric](str: String): A = {
   if A =:= Int, str.toInt
   if A =:= Float, str.toFloat
   if A =:= BigInt, BigInt(str)
   ...
   ...
}

Unfortunately, it seems that there is no fromString defined in the Numeric trait. What is the most idiomatic way to achieve this in Scala?

1 Answers
Related