Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is scala.math.PartialOrdering.lteq abstract, rather than defined in terms of .tryCompare?

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?

like image 226
Scott Morrison Avatar asked Jun 24 '12 08:06

Scott Morrison


1 Answers

My guess is to encourage writing a more efficient lteq, as all other methods fall back to lteq. So you wouldn't want to create an Option, then map it. I would rather ask the opposite -- why isn't tryCompare implemented by default, e.g.:

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
}

...and you wouldn't need to write the ugly override modifier when implementing lteq.

As far as I can see tryCompare is never ever used within the whole Scala standard library body, so maybe it's just a 'left-over'...

like image 194
0__ Avatar answered Oct 19 '22 17:10

0__