groovy Expando:为什么它考虑局部变量而不是 Expando 属性?
查看此测试代码:
def a = "test"
def expando = new Expando()
expando.a = a
expando.foobar = {a}
expando.a = "test1"
assert expando.foobar() != a
为什么最后一个断言失败?它将“a”视为局部变量,而不是 Expando.a 属性。
感谢您的帮助
looking at this test code:
def a = "test"
def expando = new Expando()
expando.a = a
expando.foobar = {a}
expando.a = "test1"
assert expando.foobar() != a
why the last assertion fail? it considers "a" as the local variable and not as the expando.a properties.
Thanks for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许我错了,但是当您调用expando.foobar()时,它返回分配给foobar的闭包结果。在本例中,它是
a
,因此它返回 a 的值:test
。expando.foobar()
不会调用属性“a”,因为闭包不会查找其委托,除非变量未在范围内定义(在本例中是这样)。编辑:
如果您要执行
expando.foobar = {delegate.a}
,则会返回您期望的结果。Perhaps I am mistaken, but when you invoke
expando.foobar()
, it returns the result of the closure that was assigned tofoobar
. In this case, it isa
, so it returns the value of a:test
.expando.foobar()
does not call the property 'a' because closures do not look for their delegate unless a variable is not defined in scope (and in this case it is).Edit:
If you were to do
expando.foobar = {delegate.a}
, that would return the results you are expecting.