This page is no longer maintained — Please continue to the home page at

invariant ordering vs. refinement lubs

No replies
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.

Since I know nobody could possibly be tired of this subject, and also
I didn't know what else to do with my test case.

class A { def f: Any }
class B extends A { def f: Int = 5 }
class C extends A { def f: Long = 5L }

object Test {
def hello[T: Ordering](t1: T, t2: T) = ???

implicit val AOrdering: Ordering[A] = ???

hello(new B, new C)
// ./a.scala:11: error: No implicit Ordering defined for A{def f: AnyVal}.
// hello(new B, new C)
// ^

hello(new B, new C)(AOrdering)
// ./a.scala:13: error: type mismatch;
// found : Ordering[A]
// required: Ordering[A{def f: AnyVal}]
// Note: A >: A{def f: AnyVal}, but trait Ordering is invariant in type T.
// You may wish to investigate a wildcard type such as `_ >: A{def
f: AnyVal}`. (SLS 3.2.10)
// hello(new B, new C)(AOrdering)
// ^

hello[A](new B, new C)
// that works of course

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland