Consulting types of arguments in first parameter list

In programming in scala 4 edition there is

Generally, when tasked to infer the type parameters of a polymorphic
method, the type inferencer consults the types of all value arguments in the
first parameter list but no arguments beyond that.

having

val abcde = List('a', 'd', 'b', 'e', 'c')
val abcde: List[Char] = List(a, d, b, e, c)
abcde sortWith (_ > _)
res0: List[Char] = List(e, d, c, b, a)

I wonder if scala consults in this case first list parameter namely β€œ(_ > )" in "abcde sortWith ( > _)” or first list parameter is assumed to be abcde

So if you take a look to the scaladoc, you will see that the signature of sortWith is as follows:

def sortWith(lt: (A, A) => Boolean): List[A]

So it doesn’t have to infer anything, since A is the type parameter of the List and since we already know that it is a List of Chars then we already know that A is Char.
So the compiler will simply type check that the function does accept two Chars and return a Bolean. And since _ > _ is expanded as (a: Char, b: Char) => a > b then it does.

1 Like