Search code examples
scalagenericscovariance

Implement Ordered trait with covariant generic


Is there a way mix in the Ordered trait for a covariant generic?

I have the following code:

trait Foo[+T  <: Foo[T]] extends Ordered[T] {

  def id: Int

  override def compare(that : T) : Int = {
    this.id compare that.id
  }    
}

Where I need T covariant and would like ordered to work too. The version above gives the "covariant type in contravariant position error".


Solution

  • You can't use Ordered with a covariant type because it requires the generic type in a contravariant position. Instead, you should use an implicit Orderingdefined in the companion object

    trait Foo[+T] {
      def id: Int
    
    }
    
    object Foo {
      implicit def fooOrdering[A <: Foo[_]]: Ordering[A] = {
        new Ordering[A] {
          override def compare(x: A, y: A): Int = x.id compare y.id
        }
      }
    }
    

    Any reasonable function that compares object should be take in an Ordering instance for the objects it is comparing, and many do implicitly. For example

    case class F(id: Int) extends Foo[Int]
    case class G(id: Int) extends Foo[Int]
    
    List(F(1), F(2), F(5), F(3), G(12)).max // = G(12)