3

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 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

So, literally you have to specify default type in this case? Oh .. I see, Looks like obvious answer. Looks like have to amend question title .. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.