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:
开发者_Python百科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.
I'd add to Rex's and Geoff's answers only one practical advice how to deal with ambiguities is to use type ascription:
val result = (thisItem._1: Integer) compareTo (thatItem._1: Integer)
Anyway, in this case using compare is probably the best solution as you are relying on Scala's types.
The error arises from the different boxing of primitive integers in Scala and Java. It's difficult for me to tell exactly what your code is doing--what type is thisItem._1
? Is it a primitive integer?
Anyway, the compareTo
method is defined on both java.lang.Integer
and scala.runtime.RichInt
(Java and Scala's boxed integers, respectively). compare is defined only on scala.runtime.RichInt
. So when you use compare, it is an unambiguous request; with compareTo
, it doesn't know which boxing to use.
精彩评论