Groovy 闭包中的默认参数

Groovy 闭包中的默认参数,groovy,closures,Groovy,Closures,看看groovy,我发现我应该能够在闭包中使用默认参数,如下所示: def closureWithTwoArgAndDefaultValue = { int a, int b=2 -> a+b } assert closureWithTwoArgAndDefaultValue(1) == 3 但是,在groovysh中运行该命令会产生以下错误: 错误groovy.lang.MissingMethodException: groovysh_evaluate.closureWithTwoAr

看看groovy,我发现我应该能够在闭包中使用默认参数,如下所示:

def closureWithTwoArgAndDefaultValue = { int a, int b=2 -> a+b }
assert closureWithTwoArgAndDefaultValue(1) == 3
但是,在
groovysh
中运行该命令会产生以下错误:

错误groovy.lang.MissingMethodException: groovysh_evaluate.closureWithTwoArgAndDefaultValue()方法的签名不适用于参数类型:(java.lang.Integer)值:[1]


有人能告诉我为什么吗?

试着省略
def

closureWithTwoArgAndDefaultValue = { int a, int b=2 -> a+b }
assert closureWithTwoArgAndDefaultValue(1) == 3

有关详细说明,请参见。

尝试省略
def

closureWithTwoArgAndDefaultValue = { int a, int b=2 -> a+b }
assert closureWithTwoArgAndDefaultValue(1) == 3

有关更多说明,请参阅。

我有
Groovy版本:2.4.7 JVM:1.8.0\u 112供应商:Oracle公司操作系统:Linux
,运行在Arch Linux上。。。你是在
groovysh
上运行的吗?我有
Groovy版本:2.4.7 JVM:1.8.0\u 112供应商:Oracle公司OS:Linux
,运行在Arch Linux上。。。你是在
groovysh
上运行的吗?看起来像是GroovyShell的东西,但我不完全理解它?您的解决方案在
groovysh
中工作,但我的问题是,我试图从Jenkins文件中运行该解决方案,结果得到
java.lang.NoSuchMethodError:在您的解决方案的步骤中找不到这样的DSL方法“closure”
。。。但这可能只是詹金斯的问题。似乎我有一个与詹金斯有关的问题,因为我可以在
groovyConsole
中运行
def
版本。@JonesV,这不是
groovyConsole
,而是
groovysh
。我的意思是,带有
def
的版本在
groovyConsole
中工作(如链接中所建议的)但不是在
groovysh
中,这就是为什么我说我的问题更像是詹金斯的问题;)看起来像是Groovy shell的东西,但我不完全理解它?您的解决方案在
groovysh
中工作,但我的问题是,我试图从Jenkins文件中运行该解决方案,结果得到
java.lang.NoSuchMethodError:在您的解决方案的步骤中找不到这样的DSL方法“closure”
。。。但这可能只是詹金斯的问题。似乎我有一个与詹金斯有关的问题,因为我可以在
groovyConsole
中运行
def
版本。@JonesV,这不是
groovyConsole
,而是
groovysh
。我的意思是,带有
def
的版本在
groovyConsole
中工作(如链接中所建议的)但不是在
groovysh
中,这就是为什么我说我的问题更像是詹金斯的问题;)