1 Jun 2009 11:32
Implicit resolution, Ordered and Ordering
Thanks to my new four day work week setup (a mixed blessing, but rather nice) I finally have time to give the collections API the kick around I've been promising.(Continue reading)I've been investigating some of the Ordering stuff to start with (I'm updating TreeSet to use Ordering, thus keeping it in line with TreeMap and giving it shiny new bonus performance goodness). I ran into some problems. Some of them are just cases where a conversion has been passed explicitly. Those are fortunately the work of moments to fix and can safely be ignored. The more problematic case is things where an existing class has explicitly extended Ordered[MyClass]. These don't automatically get an Ordering, which is a bit of a pain. Ok, I thought this would be simple to solve by just adding the following implicit to Ordering: implicit def OrderedOrdering[A <: Ordered[A]] : Ordering[A] = new Ordering[A]{ def compare(x : A, y : A) = x.compare(y); } But unfortunately this introduces an implicit ambiguity: scala> Ordering[Int] <console>:5: error: ambiguous implicit values: both value Int in object Ordering of type => Ordering[Int] and method OrderedOrdering in object Ordering of type [A <:
I've been investigating some of the Ordering stuff to start with (I'm
updating TreeSet to use Ordering, thus keeping it in line with TreeMap
and giving it shiny new bonus performance goodness). I ran into some
problems.
Some of them are just cases where a conversion has been passed
explicitly. Those are fortunately the work of moments to fix and can
safely be ignored.
The more problematic case is things where an existing class has
explicitly extended Ordered[MyClass]. These don't automatically get an
Ordering, which is a bit of a pain.
Ok, I thought this would be simple to solve by just adding the
following implicit to Ordering:
implicit def OrderedOrdering[A <: Ordered[A]] : Ordering[A] = new Ordering[A]{
def compare(x : A, y : A) = x.compare(y);
}
But unfortunately this introduces an implicit ambiguity:
scala> Ordering[Int]
<console>:5: error: ambiguous implicit values:
both value Int in object Ordering of type => Ordering[Int]
and method OrderedOrdering in object Ordering of type [A <:
RSS Feed