Preference orderings are naturally contravariant in their type parameters. Suppose that A is a sub-type of B, then a preference ordering defined over type B can be used anywhere such an ordering defined over type A is needed implying that Ordering[B] is a sub-type of Ordering[A]. In the standard library Ordering is actually invariant in its type parameter (see nice discussion on SO for the technical reasons for this as well as the possibility that it will be fixed in the future).
One way to make Preferences contravariant is as follows...
trait Preferences[-A] {
def ordering[B](implicit ev: B <:< A): Ordering[B]
}
Another approach would be to make A and upper bound.
trait Preferences[-A] {
def ordering[B <: A]: Ordering[B]
}
Preference orderings are naturally contravariant in their type parameters. Suppose that
Ais a sub-type ofB, then a preference ordering defined over typeBcan be used anywhere such an ordering defined over typeAis needed implying thatOrdering[B]is a sub-type ofOrdering[A]. In the standard libraryOrderingis actually invariant in its type parameter (see nice discussion on SO for the technical reasons for this as well as the possibility that it will be fixed in the future).One way to make
Preferencescontravariant is as follows...Another approach would be to make
Aand upper bound.