1

Looking at the groovy manual, I see that I should be able to use default parameters in closures, like so:

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

However, running that in groovysh gives me the following error:

ERROR groovy.lang.MissingMethodException: No signature of method: groovysh_evaluate.closureWithTwoArgAndDefaultValue() is applicable for argument types: (java.lang.Integer) values: [1]

Could somebody tell me why?

Opal
  • 81,889
  • 28
  • 189
  • 210
JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
  • I have `Groovy Version: 2.4.7 JVM: 1.8.0_112 Vendor: Oracle Corporation OS: Linux`, running on Arch Linux... Did you run that on `groovysh`? – JonasVautherin Oct 28 '16 at 15:18

1 Answers1

0

Try to omit def:

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

For further explanation, see here.

Community
  • 1
  • 1
Opal
  • 81,889
  • 28
  • 189
  • 210
  • Looks like Groovy shell thing, but I don't understand it completely? Your solution works in `groovysh`, but my issue is that I try to run that from a JenkinsFile, and there I get `java.lang.NoSuchMethodError: No such DSL method 'closure' found among steps` with your solution... but it might be a problem with Jenkins only. – JonasVautherin Oct 28 '16 at 15:25
  • Seems like I have an issue related to jenkins, since I can run the version _with_ `def` in `groovyConsole`... – JonasVautherin Oct 28 '16 at 15:39
  • @JonesV, it's not `groovyConsole` but `groovysh`. – Opal Oct 28 '16 at 15:49
  • I mean, the version with `def` works in `groovyConsole` (as proposed in your link) but not in `groovysh` – JonasVautherin Oct 28 '16 at 16:13
  • Which is why I said that my problem looks more like an issue in Jenkins, after all ;) – JonasVautherin Oct 28 '16 at 16:18