There is no type Number
in Scala, so I am guessing you are meaning Numeric
(Double
is not a sub-class of Numeric
, but it can be coerced to Numeric
via implicit conversion).
If so, you can achieve what you want by specifying Numeric
as a context bound for the parameter of the numberToString
function (ie. specify that there must be an in-scope implicit conversion from the argument type to Numeric
, which will generally be true for Int
, Double
, etc):
scala> def numberToString[N : Numeric] = (n: N) => n.toString()
numberToString: [N](implicit evidence$1: Numeric[N])N => String
scala> List(1.1, 2.1, 3.1).map(numberToString2)
res2: List[String] = List(1.1, 2.1, 3.1)
EDIT
Note that I am assuming here that you want to stay as much as possible in the Scala world. If you do intend restricting the argument to numberToString
to the java.lang.Number
type rather than scala.math.Numeric
, then use the similar view bounds notation as given by @wingedsubmariner.