0

I just found this function in the scala library (in the PartiallyOrdered trait):

def tryCompareTo [B >: A <% PartiallyOrdered[B]](that: B): Option[Int]

I wonder how should I exactly interpret the type bound. According to my research the >: operator is a lower type bound, and the <% is a view bound, (What are Scala context and view bounds?). However, when the two operators are combined, how do I read it.

Is it type B must be a super class of A, and A can be viewed as PartiallyOrdered[B]? (B >: (A <% PartiallyOrdered[B]), more or less grouped like this)

Or, type B must be a super class of A, and B can be viewed as PartiallyOrdered[B]? ((B >: A) <% PartiallyOrdered[B], more or less grouped like this).

An extra curiosity, can there be more than two operators combined?

mundacho
  • 229
  • 1
  • 8

3 Answers3

1

After searching a bit I found in the scaladoc at https://www.scala-lang.org/api/current/scala/math/PartiallyOrdered.html the same function is written as

abstract def tryCompareTo[B >: A](that: B)(implicit arg0: (B) ⇒ PartiallyOrdered[B]): Option[Int]

The implicit parameter corresponds to the implementation of B <% PartiallyOrdered[B] according to What are Scala context and view bounds?, thus

B can be viewed as a PartiallyOrdered[B].

mundacho
  • 229
  • 1
  • 8
1

In a type parameter list the first name in every comma delimited part is the type parameter that is being defined. All operators that follow constrain that type parameter. So the correct interpretation is type B must be a supertype of A, and B can be viewed as PartiallyOrdered[B].

Jasper-M
  • 14,966
  • 2
  • 26
  • 37
0

A can be viewed as PartiallyOrdered[B]. tryCompareTo is part of a bigger structure:

trait PartiallyOrdered[+A] 

A is the type we are talking about here

raam86
  • 6,785
  • 2
  • 31
  • 46