[英]Why am I getting a “No signature of method”" error when running the closure recursion example in the Groovy shell?
我正在嘗試從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>
這是怎么回事?
我沒有一個完美的答案,但看起來GroovySH有一些黑客可以在使用某些Groovy功能時搞砸它。
您可以在groovyConsole
(這是一個圖形編輯器,更容易玩)中完美地運行示例代碼,並使用groovy recursionTest.groovy
運行它。
我還沒有找到一個在groovy shell中正常工作的解決方案,但我還是不建議使用它來學習。
我認為你的腳本有兩個問題:
在shell環境中,您有一定的范圍。 綁定的變量位於“綁定”中。 要獲得綁定中的一個,您必須在使用它之前確保它沒有被定義 ! 所以沒有def results
。 然而,這不是錯誤。
被投的錯誤,可以通過命名您關閉遞歸固定。 結合不定義結果產生:
-
results = [];
f = { a, b ->
results << a
a<10 && call(b, a+b) }(1,1)
assert results == [1, 1, 2, 3, 5, 8, 13]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.