groovysh cannot find method
Asked Answered
P

1

6

I load the following script into groovysh:

def a() {
    println "a()"
}

def b() {
    println "b()"
    a()
}

by using :load 'test.groovy' and call method b():

b()

I get:

groovy:000> b()
b()
No signature of method: groovysh_evaluate.a() is applicable for argument types: () values: []
Possible solutions: b(), any(), is(java.lang.Object), any(groovy.lang.Closure), wait(), run()
        at groovysh_evaluate.b (groovysh_evaluate:5)

Why cannot groovysh find method a()?

Peignoir answered 28/7, 2015 at 14:49 Comment(1)
This feel a lot like a groovysh error. I too would expect that to work, it doesn't on my 2.4.0 Groovy Shell machine, but it does on this online groovy shell thing: groovyconsole.appspot.com. Hmmm. Something is not right here.Ribeiro
M
0

As a workaround you can use closures instead of methods:

a =  {
    println "a()"
}

b = {
    println "b()"
    a()
}

It gives:

groovy:000> a =  {
groovy:001>     println "a()"
groovy:002> }
===> groovysh_evaluate$_run_closure1@74a6f9c1
groovy:000> 
groovy:000> b = {
groovy:001>     println "b()"
groovy:002>     a()
groovy:003> }
===> groovysh_evaluate$_run_closure1@7922d892
groovy:000> a()
a()
===> null
groovy:000> b()
b()
a()
===> null
groovy:000> 

However no idea why it does not work.

Mcquillin answered 28/7, 2015 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.