PartialOrdering assignment in the Scala library

I understand that it is a partial order from a mathematical point of view, but cannot see where and how PartialOrdering you can use a class from the Scala library. I looked through the entire standard library and there was only one use: it was expanded by Ordering. The same story for the main method tryCompareseems to be not used anywhere.

So, what benefits can I get if instead

case class Foo(x: Int, y: String)

val order = Ordering.by { foo => (foo.x, foo.y) }

I will implement a more appropriate instance for the domain:

object PartialFoo extends PartialOrdering[Foo] {
  def tryCompare(a: Foo, b: Foo): Option[Foo] = {
    if (a.x != b.x) None  // Doesn't make sense to compare foos with different xs
    else ???
  }
}

UPD . It also looks like with a return type Some[Int](not Option[Int]) and I can’t even use redefinition. Intentionally?

+4
1

, , .

, , , .

Numeric, , , sum, - val x = List(1, 2, 3).sum. scala Numeric[Int] , "", int.

, - , "" , :

  • PartiallyOrdered - , sum,

  • PartialOrdering[T] .

PartialOrdering, Ordering, - , , , PartialOrdering, Ordering, Orderings PartialOrdering! . Int, String ..

, A

APartialOrdering extends PartialOrdering[A].

A , . ,

def doSomethingForPartiallyOrdered[T](t: T)(implicit po: PartialOrdering[T]) = _

A, Int's, Strings ..

+1

All Articles