Search code examples
scalagenericstype-inference

Scala: Type Inference Failure on Lambda of Generic Numeric Type


In Scala 2.13.10, I'm running across this problem:

import scala.reflect.ClassTag

class Test[T : Numeric : ClassTag](data: Seq[T]) {

  def map(fn: T => T): Test[T] = { new Test(this.data.map(fn)) }

  def +(y: T): Test[T] = {
    this.map(x => x + y)
  }

}

does not compile. The error is

[Error] Test.scala:10:23: type mismatch;
 found   : T
 required: String

Removing the "+" method to make it compile and using the Test.map method with e.g. t.map(x => x + y) works fine. I suspect the type inference algorithm needs a little more to go with to infer that these values are numeric and that the "+" operator in the lambda therefore isn't the string concatenation. How can I achieve that?

FWIW, IntelliJ flags the general area of the problem in the editor but with a different, weird error message. Not going to go into that as it is probably unrelated.


Solution

  • You should import the infixNumericOps to make the operator works for Numeric:

    import scala.reflect.ClassTag
    
    class Test[T : Numeric : ClassTag](data: Seq[T]) {
    
      def map(fn: T => T): Test[T] = { new Test(this.data.map(fn)) }
    
      def +(y: T): Test[T] = {
        import math.Numeric.Implicits.infixNumericOps
        this.map(x => x + y)
      }
    
    }