简体   繁体   English

Scala:BigDecimal是数字的隐式证据

[英]Scala: Implicit evidence that BigDecimal is Numeric

How does scala.math.BigDecimal is implicitly converted to scala.math.Numeric ? scala.math.BigDecimal如何隐式转换为scala.math.Numeric

For example : for Int chain of implicit conversion is traced like: Int to RichInt (conversion defined in Predef ) and method 例如 :对于Int ,隐式转换链的跟踪方式如下: IntRichInt (在Predef定义的Predef )和方法

protected implicit def num: Numeric[T]

from parent class scala.runtime.ScalaNumberProxy . 从父类scala.runtime.ScalaNumberProxy

The tricky part with scala.math.BigDecimal is that it does not have any wrappers. scala.math.BigDecimal的棘手部分是它没有任何包装。

Thanks in advance. 提前致谢。

Numeric[T] is a type class, so it doesn't convert the BigDecimal to a Numeric ; Numeric[T]是类型类,因此它不会将BigDecimal转换为Numeric instead, there is an implicit Numeric[BigDecimal] defined which contains the implementation of Numeric which operates on BigDecimal values. 相反,有一个隐式的Numeric[BigDecimal]定义,其中包含对BigDecimal值进行操作的Numeric的实现。

The implicit is defined here - https://github.com/scala/scala/blob/2.12.x/src/library/scala/math/Numeric.scala#L187 隐式定义在这里-https://github.com/scala/scala/blob/2.12.x/​​src/library/scala/math/Numeric.scala#L187

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM