Scala: Ordering contravariance

前端 未结 2 899
迷失自我
迷失自我 2021-02-05 23:23

Is there any reason why Scala\'s Ordering trait is not contravariant? A motivating example follows.

Suppose I want to perform an ordered insert. I may have

2条回答
  •  梦毁少年i
    2021-02-06 00:19

    In the current API, these methods prevent it from being contravariant:

      /** Return `x` if `x` >= `y`, otherwise `y`. */
      def max(x: T, y: T): T = if (gteq(x, y)) x else y
    
      /** Return `x` if `x` <= `y`, otherwise `y`. */
      def min(x: T, y: T): T = if (lteq(x, y)) x else y
    

提交回复
热议问题