I’ve got the following class hierarchy:
class A[T](val value: T)
class B[T](value: T) extends A[T](value)
I want to define implicit conversions and orderings for the hierarchy to be able to compare instances the following way:
new A(1) < new A(2)
new B(2) > new A(1)
and so on.
Orderding should rely on value field ordering.
new B(2) > A(1) because new B(2).value > new A(1).value
Please, help!
You don’t tell what should happen between
B(1)andA(1). Supposing they are equalSubtype
Bis mostly irrelevant, the ordering is ofA, and is has to order instances ofBtoo, as they are instance ofA. Even if you decide being aBchanges the comparison (maybe it breaks the tie) that would force you to write a custom ordering, but still at the same place, with the same signature.Of course, the ordering is only defined when an orderign is available on
T(hence[T : Ordering]). And it is best to define it in companionobject A, which will make it available all the time, without import.