我有一段代碼,我用它來提供一個優先級隊列的順序隱:在這個Scala代碼中,compareTo與compareTo有什麼不同?
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
}
}
下面這段代碼上的Scala 2.7無法編譯 - 錯誤信息是:
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
^
我發現了兩個辦法讓它編譯 - 要麼宣佈是詮釋的結果的類型或改變使用compareTo至比較。但我的問題是 - 是否有這樣一個錯誤的原因,這個消息意味着什麼,這是一個在scala編譯器中的錯誤? compareTo只調用Ordered [A]特徵中的比較,並且具有相同的特徵...再加上它們都返回Int,那麼爲什麼我自己聲明類型是重要的?