高阶函数定义中的括号错误(Scala)

Error for parentheses in higher order function definitions (Scala)

我在高阶定义中遇到圆括号错误。以下代码工作正常:

val foo: Int => (Int => Int) = n => n + _*2

但是,添加括号后出现编译错误

val foo1: Int => (Int => Int) = n => n + (_*2)

Error:(34, 56) missing parameter type for expanded function ((<x: error>) => x.$times(2))

我知道我可以使用另一种样式来避免错误:

val bar = (x: Int) => (y: Int) => x + (y*2)

我感兴趣的是括号有什么问题,以及如何在格式化高阶函数的相同风格中正确使用它们

匿名函数占位参数第一种情况

val foo: Int => (Int => Int) = 
  n => n + _ * 2

扩展到

val foo: Int => (Int => Int) =
  (x: Int) => (n: Int) => n + x * 2

而第二个

val foo1: Int => (Int => Int) = 
  n => n + (_ * 2)

扩展到

val foo1: Int => (Int => Int) = 
  n => n + (x => x * 2)

这是语法错误。关键是理解:

If the underscore is inside an expression delimited by () or {}, the innermost such delimiter that contains the underscore will be used;