0

作为https://www.scala-lang.org/api/current/scala/collection/immutable/List.html#sortWith(lt:(A,A)=%3EBoolean)

List("Steve", "Tom", "John", "Bob").sortWith(_.compareTo(_) < 0) = List("Bob", "John", "Steve", "Tom")

sortWith 的参数是:

lt: (A, A) => Boolean

其中具有两个参数并返回布尔值的函数

但是 compareTo 只有一个参数:

def compareTo(that: A): Int

如果 compareTo 是一个函数,它有两个参数。但是它是 scala,所以 compareTo 是一个方法,它只有一个参数

那为什么sortWith中可以使用compareTo呢?似乎不符合sortWith的类型签名

4

1 回答 1

3

下划线的这种用法称为匿名函数的占位符语法

这样的表达式表示一个匿名函数,其中随后出现的下划线表示连续的参数。

请注意,每个下划线指的是不同的参数,例如

_ + _

扩展到

(x1, x2) => x1 + x2

表达式_ + _也使用无点语法,而不是

_.+(_)

例如

List("Steve", "Tom", "John", "Bob").reduce((x1: String, x2: String) => x1.+(x2)) // : String = "SteveTomJohnBob"
List("Steve", "Tom", "John", "Bob").reduce((x1, x2) => x1.+(x2)) // : String = "SteveTomJohnBob"
List("Steve", "Tom", "John", "Bob").reduce(_.+(_)) // : String = "SteveTomJohnBob"
List("Steve", "Tom", "John", "Bob").reduce(_ + _) // : String = "SteveTomJohnBob"

所以现在应该更清楚为什么表达_.compareTo(_) < 0有效

List("Steve", "Tom", "John", "Bob").sortWith(_.compareTo(_) < 0) // : List[String] = List("Bob", "John", "Steve", "Tom")
List("Steve", "Tom", "John", "Bob").sortWith((x1, x2) => x1.compareTo(x2) < 0) // : List[String] = List("Bob", "John", "Steve", "Tom")

另一种看待这一点的方式让我们对含糖的表达式进行类型归因

scala> (_.compareTo(_) < 0): ((String, String) => Boolean)
val res0: (String, String) => Boolean = $Lambda$7865/418832725@18987bf5
于 2021-05-12T20:39:56.597 回答