I'm trying to use Scala tuples in a Java collection library that requires its elements to be Comparable
. I know that recent versions of Scala allow lexicographical ordering of tuples through math.Ordering.Implicits
. However, it looks like there exists no default conversion from Scala's Ordering[T]
to Java's Comparable[T]
. Whether or not I've imported tuple orderings, I get the error java.lang.ClassCastException: scala.Tuple2 cannot be cast to java.lang.Comparable
.
Is there a way to automatically convert an Ordered
or Ordering
type to Comparable
? If not, is it possible to more specifically make tuples Comparable
in a lexicographical fashion? I'm looking for something better than creating a custom case class that implements Comparable
for more generality and to reduce boiler-plate and typographical noise ((a, b, c)
is much cleaner and easier to type than BeanLikeClassWithComparable(a, b, c)
).
EDIT:
I've attempted to use J Cracknell's solution, but to no avail (I receive the same error as above). I've posted an attempted implementation of the Wrapped
class; it's a less general implementation for pairs and doesn't seem to work.
import scala.math.Ordering.Implicits._
case class OrderedPair[A <: Ordered[A], B <: Ordered[B]](elem: (A, B))
extends Ordered[OrderedPair[A, B]] {
override def compare(that: OrderedPair[A, B]): Int =
implicitly[Ordering[(A, B)]].compare(this.elem, that.elem)
}
implicit def tuple2ToOrdered[A <: Ordered[A], B <: Ordered[B]](x: (A, B)) = OrderedPair(x)