我正在玩闭包并看到这种我无法解释的奇怪行为:
groovy:000> ({ println owner })()
groovysh_evaluate@200b6145
===> null
groovy:000> ({ println "${owner}" })()
groovysh_evaluate@2bf75a70
===> null
groovy:000> ({ ({ println owner })() })()
groovysh_evaluate$_run_closure1@10f67a01
===> null
groovy:000> ({ ({ println "${owner}" })() })()
ERROR java.lang.StackOverflowError:
null
at groovysh_evaluate$_run_closure1_closure2.doCall (groovysh_evaluate:2)
at groovysh_evaluate$_run_closure1_closure2.doCall (groovysh_evaluate)
at groovysh_evaluate$_run_closure1.doCall (groovysh_evaluate:2)
at groovysh_evaluate$_run_closure1_closure2.doCall (groovysh_evaluate:2)
at groovysh_evaluate$_run_closure1_closure2.doCall (groovysh_evaluate)
at groovysh_evaluate$_run_closure1.doCall (groovysh_evaluate:2)
<stacktrace repeats>
我认为这与它本身是一个闭包的事实有关${}
,但我不能完全确定为什么会发生这种情况。这个问题似乎与访问有关,owner
因为我没有看到它发生在其他变量/表达式中。有任何想法吗?