2010-12-23 20 views
2
trait Ordered[A] extends java.lang.Comparable[A] { 
    def compare(that: A): Int 
    def < (that: A): Boolean = (this compare that) < 0 
    def > (that: A): Boolean = (this compare that) > 0 
    def <= (that: A): Boolean = (this compare that) <= 0 
    def >= (that: A): Boolean = (this compare that) >= 0 
    def compareTo(that: A): Int = compare(that) 
} 

同時擁有comparecompareTo是不是有點無用? 我在這裏失去的巨大好處是什麼?Ordered [A]爲什麼使用比較方法而不是重用compareTo?

如果他們剛剛使用compareTo我可以在我的代碼中將Comparable替換爲Ordered並完成。

+0

請問訂購前期可比?這很奇怪,但至少有一個實現調用另一個。 – 2010-12-23 18:19:26

+0

命名法。這只是Scala與大Java不同的方式。 – 2010-12-23 20:07:49

回答

10

我認爲這是一個歷史性的事故。 Ordered原本沒有從Comparable繼承。一旦確定,compare名稱已經建立。

1

我認爲Scala庫的作者只是比較喜歡compare()。

相關問題