Tuesday, 27 August 2013

Trouble "overriding" values in a Groovy closure with .delegate

Trouble "overriding" values in a Groovy closure with .delegate

I'd like to call a closure with a delegate parameter to override or shadow
the calling context. But the following example prints prints "outside"
where I expect "inside".
What am I doing wrong?
def f(String a){
def v = { return a }
v.delegate = [a:"inside"]
// Makes no difference:
// v.resolveStrategy = Closure.DELEGATE_FIRST
println(v.call())
}
f("outside")

No comments:

Post a Comment