以下多态方法编译:
import spire.math._
import spire.implicits._
scala> def foo[A:Numeric](d : A) = 2 * d
foo: [A](d: A)(implicit evidence$1: spire.math.Numeric[A])A
scala> def foo[A:Numeric](d : A) = 2 + d
foo: [A](d: A)(implicit evidence$1: spire.math.Numeric[A])A
但是,如果我将整数更改2
为双精度2.0
,编译器会抱怨找不到隐式值
scala> def foo[A:Numeric](d : A) = 2.0 + d
<console>:19: error: could not find implicit value for parameter ev: spire.algebra.Field[A]
def foo[A:Numeric](d : A) = 2.0 + d
^
scala> def foo[A:Numeric](d : A) = 2.0 * d
<console>:19: error: could not find implicit value for parameter ev: spire.algebra.Field[A]
def foo[A:Numeric](d : A) = 2.0 * d
^
我试图理解关于 SO 的其他一些问题和答案,但我对如何解决这个问题并不明智。