开发者

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

开发者 https://www.devze.com 2023-02-01 04:07 出处:网络
trait Ordered[A] extends java.lang.Comparable[A] { def compare(that: A): Int def <(that: A): Boolean = (this compare that) <0
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

精彩评论

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