最佳答案
它对于作为参数传递给高阶函数很有用。例如,用相同的元素替换列表中的所有元素:
scala> List(1, 2, 3, 4, 5).map(Function.const(7))
res1: List[Int] = List(7, 7, 7, 7, 7)
你当然也可以写
scala> List(1, 2, 3, 4, 5).map(_ => 7)
res2: List[Int] = List(7, 7, 7, 7, 7)
根据上下文,一个可能比另一个更具可读性。
关于function - Function.const 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5924875/