8

It seems that scala.math.PartialOrdering.lteq must always be defined as (or at least, give the same result as):

override def lteq(x: Pattern, y: Pattern) = {
    tryCompare(x, y).map(_ <= 0).getOrElse(false)
}

Is there some reason this implementation isn't given in the scala.math.PartialOrdering trait?

4

1 回答 1

2

我的猜测是鼓励编写更有效lteq的方法,因为所有其他方法都回退到lteq. 所以你不想创建一个Option,然后映射它。我宁愿反问——为什么tryCompare默认不实现,例如:

def tryCompare(x: T, y: T) = {
  val p1 = lteq(x, y)
  val p2 = lteq(y, x)
  if (p1) {
    if(p2) Some(0) else Some(-1)
  } else if (p2) Some(1) else None
}

...并且您在实现时不需要编写丑陋的override修饰符lteq.

据我所知,tryCompare在整个 Scala 标准库主体中从未使用过,所以也许它只是一个“剩余的”......

于 2012-06-24T20:33:39.047 回答