Search code examples
groovygroovyshell

Closure error on groovysh


Run groovysh v2.3.6:

groovy:000> def f = {x -> x}
groovy:000> f(1)
ERROR groovy.lang.MissingMethodException:
No signature of method: groovysh_evaluate.f() is applicable for argument types: (java.lang.Integer) values: [1]
Possible solutions: is(java.lang.Object), run(), run(), find(), any(), any(groovy.lang.Closure)

Is this error a known bug for closure on groovysh?


Solution

  • Yes, this is a known issue, using def in groovysh doesn't work like you'd expect. Variables declared with def or with a datatype don't get stored in the GroovyShell's binding, which is where the shell is looking for the names entered in the repl.

    It will work if you declare the variable without def like this:

    f = { x -> x }