title: Extend scala class that extends ordered
body: <p>I'm having trouble extending a base class that extends Ordered[Base]. My derived class can't extend Ordered[Derived] so can't be used as a key in a TreeMap. If I create a TreeMap[Base] and then just override compare in Derived that works but it's not what I want. I would like to be able to have the derived class as a key. Is there a way around this?</p>
  
  <pre><code>case class A(x: Int) extends Ordered[A] {
    def compare(that: A) = x.compare(that.x)
  }
  
  // Won't compile
  //  case class B(val y : Int) extends A(1) with Ordered[B] {
  //    def compare(that: B) = x.compare(that.x) match {
  //      case 0 =&gt; y.compare(that.y)
  //      case res =&gt; res
  //    }
  //  }
  
  // Compiles but can't be used to define a TreeMap key
  case class B(y: Int) extends A(1) {
    override def compare(that: A) = that match {
      case b: B =&gt; x.compare(b.x) match {
        case 0 =&gt; y.compare(b.y)
        case res =&gt; res
      }
      case _: A =&gt; super.compare(that)
    }
  }
  
  def main(args: Array[String]) {
    TreeMap[B, Int]() // Won't compile
  }
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p><a href="http://osdir.com/ml/lang.scala/2007-03/msg00128.html" rel="nofollow noreferrer">This discussion</a> on the scala mailing list seems to be very relevant but it loses me a bit.</p>