_ 在这个已经采用默认值的方法中意味着什么?
What does _ mean in this method which already takes a default value?
我正在开发一个测试套件,它有一个辅助方法,例如:
def setupMocks(isChild: Boolean = false): Unit
在一些测试中,它被调用为:
setupMocks(_)
而在其他测试中,它被调用为:
setupMocks()
_
在这里做什么?有什么意义?我确实尝试了调试器,但它只是跳过了,我无法弄清楚。
因为有一个默认参数,你可以把它当作一个有 1 个参数和 0 个参数的方法(有点)。
下划线基本上是函数参数的占位符。对于 x => setupMocks(isChild=x)
,setupMocks(_)
是 shorthand。参见 What are all the uses of an underscore in Scala?。
第二个例子是一个普通的方法调用,用 isChild=false
.
调用
我正在开发一个测试套件,它有一个辅助方法,例如:
def setupMocks(isChild: Boolean = false): Unit
在一些测试中,它被调用为:
setupMocks(_)
而在其他测试中,它被调用为:
setupMocks()
_
在这里做什么?有什么意义?我确实尝试了调试器,但它只是跳过了,我无法弄清楚。
因为有一个默认参数,你可以把它当作一个有 1 个参数和 0 个参数的方法(有点)。
下划线基本上是函数参数的占位符。对于 x => setupMocks(isChild=x)
,setupMocks(_)
是 shorthand。参见 What are all the uses of an underscore in Scala?。
第二个例子是一个普通的方法调用,用 isChild=false
.