为什么我得到一个“没有方法的签名”错误当运行封闭递归示例在Groovy shell? [英] Why am I getting a "No signature of method"" error when running the closure recursion example in the Groovy shell?

查看:452
本文介绍了为什么我得到一个“没有方法的签名”错误当运行封闭递归示例在Groovy shell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试使用来自 http://groovy.codehaus.org的Groovy闭包递归示例/ JN2515-Closures

我将该代码段保存在一个名为recursionTest.groovy的文件中,并将其加载到shell中,签名方法错误:

I saved the snippet in a file called recursionTest.groovy and loaded it in the shell, but I'm getting a "No signature of method error":

// 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> 

发生了什么事?

推荐答案

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

I think there are two problems in your script :


  1. 一定范围。绑定的变量在绑定。要在绑定中获得一个,您必须在使用它之前看到它未定义!所以没有 def results

的错误可以通过命名您的闭包 / strong>递归。结合未定义结果的结果为:

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]

这篇关于为什么我得到一个“没有方法的签名”错误当运行封闭递归示例在Groovy shell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆