I want to be able to define the following function in Scala:
import Numeric.Implicits._
def f[T:Numeric](a:T) = a*2
but I get the error
error: could not find implicit value for parameter num: scala.math.Numeric[Any]
def f[T:Numeric](a:T) = a*2
^
I can get it to work with an implicit n:Numeric[T] argument and n.fromInt, but readability suffers. (The real code I'm trying to write is more complicated than this.)
I tried to define an implicit conversion from Int to Numeric[T]:
implicit def intToNumericT[T](x:Int)(implicit n:Numeric[T]):T = n.fromInt(x)
but that doesn't help. Is there a way to get the above code to work?