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?