3
votes

How to specify default values for the type parameter in this context?

def increase[T: Numeric](x: T, y: T): T = implicitly[Numeric[T]].plus(x, y)

val inc = increase _

Output:

C:\Sources\scala\main.scala:12: error: could not find implicit for evidence parameter of type Numeric[Nothing] val inc = increase _

1

1 Answers

2
votes

increase has a generic type parameter. When you attempt to resolve the method to a function, it is implicitly trying to lookup the type T for which it needs to resolve the method. Since you haven't specified any type, it attempts to look up Numeric[Nothing] and finds out there is no such implicit available in scope.

What you need is to explicitly specify the type T for each resolution:

scala> val intInc = increase[Int] _
inc: (Int, Int) => Int = <function2>

scala> val doubleInc = increase[Double] _
doubleInc: (Double, Double) => Double = <function2>