Groovy:闭包中“ this”的含义 [英] Groovy: meaning of 'this' inside a closure

查看:154
本文介绍了Groovy:闭包中“ this”的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例摘自行动中的时髦

The following example is adapted from 'Groovy in Action'

class Mother {

    Closure birth() {                            
        def closure = { caller ->
            [this, caller]
        }
        return closure
    }
}                    

Mother julia = new Mother()
closure = julia.birth()                                
context = closure.call(this)                             

println context[0].class.name        // Will print the name of the Script class
assert context[1] instanceof Script 

根据这本书, this的值闭包内部的是最外面的范围(即声明 julia 的范围)。我是否假设

According to the book, the value of this inside the closure is the outermost scope (i.e. the scope in which julia is declared). Am I right in assuming that


  • 在闭包内的计算范围是正确的

  • 在上面显示的关闭中, this caller 指的是同一作用域?

  • this inside a closure evaluates to the scope in which the closure is called?
  • within the closure shown above, this and caller refer to the same scope?

谢谢,
Don

Thanks, Don

推荐答案

this 在Groovy中总是意味着(无论是常规的Java块还是Closure),周围的阶层(实例)。 owner 是闭包的属性,指向嵌入对象,该对象可以是一个类(实例),然后与 或另一个闭包。我会完全忘记这部分的范围。因此,在上述情况下, this是指母亲是正确的。

"this" in a block mean in Groovy always (be it a normal Java-like block or a Closure) the surrounding class (instance). "owner" is a property of the Closure and points to the embedding object, which is either a class (instance), and then then same as "this", or another Closure. I would forget about the scope thing totally for this part. So in the case above it is correct, that "this" refers to a mother.

现在让事情变得复杂起来…… this和隐式this是在Groovy中不一样。因此,如果您有一个闭包 {foo()} {this.foo()} ,您将获得不同的结果。 this.foo()将始终解析为嵌入类,而仅 foo()将使用Groovy元对象协议(MOP)可以指向完全不同的内容。例如,对于标准的Groovy构建器,构建器可以在该Closure上设置委托,并捕获方法调用。

And now to make things complicated... "this" and the implicit this are not the same in Groovy. So if you have a Closure {foo()} and {this.foo()} you can get differing results. this.foo() will always be resolved to the embedding class, while only foo() will be resolved using the Groovy meta object protocol (MOP) and can point to something entirely different. A builder may for example set a delegate on that Closure and catch the method invocation, for a Groovy builder that is standard. Anyway... that is why this part is called dynamic scoping.

历史背景:
在Groovy 1.0之前, this是Closure对象本身。但是更改了,因为如果构建器确实捕获了所有调用,则实际上无法调用 this.foo()。那么您将无法再从构建器内部调用本地方法。尝试了很多改变标准解决策略的尝试-以及激烈的讨论。但是最后,更改 this以引用嵌入类是解决该问题的简单方法,并且与Java来的人们更加一致,如果您坚持的话,可以轻松绕过MOP。

Historic background: Before Groovy 1.0 "this" was the Closure object itself. But was changed because actually calling this.foo() became impossible if a builder did capture all calls. then you had no way to call local methods from within the builder anymore. There was a lot of tries with changing the standard resolve strategy - and big emotional discussions too. But in the end, changing "this" to refer to the embedding class was a simple solution to the problem and is more in line with people coming from Java plus it let's you easily bypass the MOP if you insist.

这篇关于Groovy:闭包中“ this”的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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