Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering[T] and friends should be contravariant #7179

Closed
scabug opened this issue Feb 25, 2013 · 3 comments
Closed

Ordering[T] and friends should be contravariant #7179

scabug opened this issue Feb 25, 2013 · 3 comments

Comments

@scabug
Copy link

scabug commented Feb 25, 2013

The Ordering[T] trait and its friends Equiv[T] and PartialOrdering[T] should be contravariant in T. This would for example allow SeqLike.sorted to be declared as:

def sorted(implicit ord: math.Ordering[A]): Repr

rather than

def sorted[B >: A](implicit ord: math.Ordering[B]): Repr

It would also provide a good didactic example of contravariance in the library!

@scabug
Copy link
Author

scabug commented Feb 25, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7179?orig=1
Reporter: Dan Rosen (mergeconflict)

@scabug
Copy link
Author

scabug commented Mar 2, 2013

Dan Rosen (mergeconflict) said:
I spent a little time tinkering with this and found two issues:

  • Ordering[T] defines methods max and min, which return T (in covariant position).
  • Ordering[T] extends java.util.Comparator[T] which, having been defined in Java, is invariant in T.

The first problem seems easy to deal with. The second, though, well... I wish Scala could somehow infer correct variance for Comparator.

@scabug
Copy link
Author

scabug commented Mar 7, 2013

@vigdorchik said:
It's not possible to have Ordering contravariant until we drop the compatibility with java.util.Comparator, which is going to be a major change that needs good reason.
It's also not possible to declare an implicit view, since we don't control the implicit scope of Comparator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants