Why am I getting a “No signature of method”\" error when running the closure recursion example in the Groovy shell?

☆樱花仙子☆ 提交于 2019-12-05 16:59:56

I think there are two problems in your script :

  1. In a shell environment you have a certain scope. The variables that are bound are in the "binding". To get one in the binding you must see to it that it's NOT DEFINED before you use it! So no def results. That's not the error that is cast however.

  2. The error that is cast can be fixed by naming your closure recursion. That combined with not defining the results yields :

-

results = []; 

f = { a, b ->   
results << a   
a<10 && call(b, a+b) }(1,1)

assert results == [1, 1, 2, 3, 5, 8, 13]

I don't have a perfect answer for you, but it looks like GroovySH has some hacks that can screw it up when working with certain Groovy features.

The example code you have works perfectly in groovyConsole (which is a graphical editor, and much easier to play around in), as well as running it using groovy recursionTest.groovy.

I haven't found a solution that works correctly in the groovy shell, but I wouldn't really recommend using that for learning, anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!