Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala: default values for function with type parameters; used in partial applied context

Tags:

scala

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 _

like image 787
Pavel Avatar asked Jun 20 '26 18:06

Pavel


1 Answers

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>
like image 193
Yuval Itzchakov Avatar answered Jun 23 '26 09:06

Yuval Itzchakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!