Scala:BigDecimal 是数字的隐含证据

Scala: Implicit evidence that BigDecimal is Numeric

scala.math.BigDecimal如何隐式转换为scala.math.Numeric

例如:对于Int隐式转换链被跟踪为:IntRichInt(转换定义在Predef) 和方法

protected implicit def num: Numeric[T]

来自 parent class scala.runtime.ScalaNumberProxy.

scala.math.BigDecimal 的棘手部分是它没有任何包装器。

提前致谢。

Numeric[T] 是一种类型 class,因此它不会将 BigDecimal 转换为 Numeric;相反,有一个隐含的 Numeric[BigDecimal] 定义,其中包含对 BigDecimal 值进行操作的 Numeric 的实现。

这里定义了隐式 - https://github.com/scala/scala/blob/2.12.x/src/library/scala/math/Numeric.scala#L187