Hi,
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
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
inside a closure evaluates to the scope in which the closure is called?- within the closure shown above,
this
andcaller
refer to the same scope?
Thanks, Don