开发者

Why does Ordered[A] use a compare method instead of reusing compareTo?

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)
}

Isn't it a bit useless to have both compare and compareTo? What is the huge benefit I'm missing here?

If they had just used compareTo I could just had replaced Comparable with Ordered in my code and be done.


I think it's a historic accident. Ordered originally did not inherit from Comparable. Once it did, the compare name was already established.


I assume the authors of the Scala libraries just prefer the name compare().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜