I have a trait
trait Weight {
def getWeight: Int
}
Multiple classes inherits it, example:
case class Test(n: Int) extends Weight {
override def getWeight: Int = n
}
Now i want to add sorting ability to all Weight
subclasses. I added Ordered
to Weight
:
trait Weight extends Ordered[Weight] {
def getWeight: Int
override def compare(that: Weight): Int = this.getWeight.compareTo(that.getWeight)
}
Try sorting:
val seq = Seq(Test(1), Test(4), Test(3), Test(2))
seq.sorted // error
And it's not compiles:
Error:(74, 6) diverging implicit expansion for type scala.math.Ordering[A$A254.this.Test] starting with method $conforms in object Predef seq.sorted;} ^
Whats i am doing wrong?
sorted
, which implicitly uses Ordering raises error. – zellaTest(1).compareTo(Test(2))
and it's ok – zella