Why does Numeric behave differently than Ordered?

后端 未结 3 1672
無奈伤痛
無奈伤痛 2021-02-14 09:23

Scala has a number of traits that you can use as type classes, for example Ordered and Numeric in the package scala.math.

I can, f

3条回答
  •  感情败类
    2021-02-14 10:15

    The symbolic operators are available if you import them from the implicit Numeric[T]

    def g[T : Numeric](a: T, b: T) = {
      val num = implicitly[Numeric[T]]
      import num._
      a * b
    }
    

    This is clearly a bit unwieldy if you want to make use of just a single operator, but in non-trivial cases the overhead of the import isn't all that great.

    Why are the operators not available without an explicit import? The usual considerations against making implicits visible by default apply here, perhaps more so because these operators are so widely used.

提交回复
热议问题