Skip to content

Make the Preferences trait contravariant in its type parameter #17

@davidrpugh

Description

@davidrpugh

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]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions