为什么部分应用程序需要指定参数类型?

Why does partial application require to specify a parameter type?

为什么 Scala 中的部分函数应用程序需要提供类型,例如:

def func(a: Int, b: Int) = ???

def func1 = func(_ : Int, 1) // compiles fine

def func1x = func(_, 1) // does not compile
// error: missing parameter type for expanded function ((x) => func(x, 1))

为什么在这种情况下没有推断出类型?推断类型会导致语法复杂或模棱两可,还是类型可能不像我看起来那么清晰?

从编译错误可以看出 func(_, 1) 扩展为 x => func(x, 1)。如果您首先写 def f = x => func(x, 1),则无法保证 xIntSLS 6.23.1 不幸的是,在没有明确给出类型描述的情况下,什么也没说。

List(1, 2, 3).map(_ + 1) 有效,因为 map 需要 Int => Int 的参数。 def func1x = func(_, 1) 不起作用,因为 func1x 的类型是从右侧推断出来的,但技术上也不知道。只有推断 x: Int => f(x, 1) 才有意义,但我猜想这会增加另一个不必要的复杂性,因为编译器必须处理方法参数上占位符语法的极端情况。