Cannot provide implicit conversion from DateTime to Ordered using implicit conversion in Comparable

I am trying to use> =,> etc. using DateTime (joda), and the only way to make it work is to use this implicit conversion

implicit def dateTime2ordered(x: DateTime): Ordered[DateTime] = new Ordered[DateTime] with Proxy { val self = x def compare(y: DateTime): Int = { x.compareTo(y) } } 

I would prefer a more general form like

 implicit def comparable2ordered[A <: Comparable[A]](x: A): Ordered[A] = new Ordered[A] with Proxy { val self = x def compare(y: A): Int = { x.compareTo(y) } } 

But the compiler cannot find this conversion, and after trying to call it directly, I got the following message stating that DateTime is not of type Comparable [A]. After checking the DateTime source, I saw that it only implements Comparable as a raw type.

I managed to get it to work using

  implicit def comparable2ordered[A <: Comparable[_]](x: A): Ordered[A] = new Ordered[A] with Proxy { val self = x def compare(y: A): Int = { x.compareTo(y) } } 

My question is: Is this the correct Scala handling of this problem, or is the associated wildcard type causing future type checking problems?

+7
source share
3 answers

Great, the raw type "Comparable" translates to "Comparable [_]" in Scala.

They are called existential types, Comparable [_] is short for "Comparable [T] forSome {type T}" (since version 2.7 see http://www.scala-lang.org/node/43 )

See also "Existential Types" at http://www.artima.com/scalazine/articles/scalas_type_system.html

+2
source

I came across this question because I also wanted to compare joda DateTime objects using relational operators.

Daniel answered me in the right direction: the implications present in scala.math.Ordered convert an instance of A extends java.lang.Comparable[A] to Ordered[A] - they just need to be put into scope. The easiest way to do this (which I learned here , btw) is via the implicitly method:

 val aOrdering = implicitly[Ordering[A]] import aOrdering._ 

The conclusion is that org.joda.time.DateTime does not extend or implement Comparable itself; it inherits (indirectly) from org.joda.time.ReadableInstant , which extends Comparable . So:

 val dateTimeOrdering = implicitly[Ordering[DateTime]] import dateTimeOrdering._ 

will not compile because DateTime does not extend Comparable[DateTime] . To use the Ordered relational operators on DateTime , you should do this instead:

 val instantOrdering = implicitly[Ordering[ReadableInstant]] import instantOrdering._ 

which works because ReadableInstant extends Comparable[ReadableInstant] , and implicit conversions in Ordered can convert it to Ordered[ReadableInstant] .

So far so good. However, there are situations when Ordered[ReadableInstant] not good enough. (I came across ScalaTest bigger and smaller than Matchers .) To get Ordered[DateTime] , I was forced to do this:

 implicit object DateTimeOrdering extends Ordering[DateTime] { def compare(d1: DateTime, d2: DateTime) = d1.compareTo(d2) } 

There seems to be an easier way, but I could not figure it out.

+6
source

You see, the fact is that it already exists. Well, sort of ... If you look inside an Ordered object where implicit conversions are searched, you will find the following:

 implicit def orderingToOrdered [T] (x: T)(implicit ord: Ordering[T]) : Ordered[T] 

So, as long as Ordering[T] exists, you can create Ordered[T] . Now, to search for Ordering[T] inside the Ordering object:

 implicit def ordered [A] (implicit arg0: (A) β‡’ Comparable[A]) : Ordering[A] 

So, if you pass comparable: A with Comparable[A] to the expected Ordered[A] , it will do the following:

 Ordered.orderingToOrdered(comparable)(Ordering.ordered(Predef.identity(comparable))) 

Now, for your question: using existential types is the right way to handle Java types. Theoretically, this is possible in order to lead to incorrect ordering, but in practice it is extremely unlikely. You may have problems with implicit ambiguity, since Scala already has an implicit Comparable => Ordered conversion, as shown above.

+2
source

All Articles