为什么我在Groovy shell中运行闭包递归示例时遇到“No signature of method”“错误?

前端之家收集整理的这篇文章主要介绍了为什么我在Groovy shell中运行闭包递归示例时遇到“No signature of method”“错误?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试从 http://groovy.codehaus.org/JN2515-Closures开始尝试Groovy闭包递归示例.

我将片段保存在一个名为recursionTest.groovy的文件中并将其加载到shell中,但我得到了“没有签名方法错误”:

// recursionTest.groovy   

def results = [];
{ a,b ->
  results << a
  a<10 && call(b,a+b)
}(1,1)

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


groovy:000> load recursionTest.groovy
===> []
ERROR groovy.lang.MissingMethodException:
No signature of method: java.lang.Boolean.call() is applicable for argument types: (groovysh_evaluate$_run_closure1) values: [groovysh_evaluate$_run_closure1@6b7599cc]
Possible solutions: wait(),any(),wait(long),and(java.lang.Boolean),each(groovy.lang.Closure),any(groovy.lang.Closure)
        at groovysh_evaluate.run (groovysh_evaluate:1)
        ...
groovy:003>

这是怎么回事?

解决方法

我认为你的脚本有两个问题:

>在shell环境中,您有一定的范围.绑定的变量位于“绑定”中.要获得绑定中的一个,您必须在使用它之前确保它没有被定义!所以没有def结果.然而,这不是错误.
>可以通过命名闭包递归来修复强制转换的错误.结合不定义结果产生:

results = []; 

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

猜你在找的Groovy相关文章