I have a piece of code that I use to provide an implicit ordering for a priority queue:
type Time = Int
type Item = (Time, Whatever)
implicit def order(thisItem: Item): Ordered[Item] =
new Ordered[Item] {
override def compare(thatItem: Item) = {
val result = thisItem._1 compareTo thatItem._1
-result
}
}
Now this code doesn't compile on Scala 2.7 - the error message is:
error: type mismatch;
found : SimulationMode.this.Time
required: ?{val compareTo: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method int2Integer in object Predef of type (Int)java.lang.Integer
and method intWrapper in object Predef of type (Int)scala.runtime.RichInt
are possible conversion functions from SimulationMode.this.Time to ?{val compareTo: ?}
val result = thisItem._1 compareTo thatItem._1
^
I found two ways to make it compile - either declare the type of result to be Int or change the use of compareTo to compare. But my question is - is there a reason for such an error, what does the message mean and is this a bug in the scala compiler? compareTo just calls compare in the Ordered[A] trait and has the same signature... Plus both of them return Int, so why does it matter that I declare the type myself?
This happens because an Int does not have a compareTo
method just as the error says. Further when searching for an implicit that makes this work, the compiler found an ambiguity between a conversion to java.lang.Integer
and scala.runtime.RichInt
both of which provide a compareTo
method. When you use compare
instead of compareTo
it works because only the RichInt
conversion provides that method. FYI compare
is from scala's Ordered
trait.
I'm not sure what you mean when you say it works when you declare the type of result, that shouldn't make a difference, you'd have to post the code you're using that seems to make it work.