I want to make the following trait contravariant in its type parameter.
trait Preferences[-A] {
def ordering: Ordering[A]
}
the issue with the above solution is that in Scala standard library Ordering
is invariant in its type parameter. This SO post has a discussion of why this is the case.
I have developed two solutions to my problem. The first solution is to make the parameter A
and upper bound.
trait Preferences[-A] {
def ordering[B <: A]: Ordering[B]
}
The second is to use implicit
.
trait Preferences[-A] {
def ordering(implicit ev: B <:< A): Ordering[B]
}
Both of these compile, but I don't understand the trade-offs. Is one of these approaches more general? Is there a third approach that I should be using instead?