If I have a closure attached to an expando and the closure references a value on the expando like so...
def e = new Expando()
e.val = 'hi'
e.doit = { println delegate.val }
e.doit()
It works fine. and prints 'hi'
If I call the closure with the long form
e.doit.call()
It throws an error
groovy.lang.MissingPropertyException: No such property: val for class: Script1
at Script1$_run_closure1.doCall(Script1.groovy:4)
at Script1$_run_closure1.doCall(Script1.groovy)
at Script1.run(Script1.groovy:6)
This happens because the delegate is changed from e to the script. Why? I thought that e.doit() and e.doit.call() are supposed to be the same.
I can change the delegate manually - like so
def e = new Expando()
e.val = 'hi'
e.doit = { println delegate.val }
e.doit.delegate=e;
e.doit.call()
Any ideas on how to skip the explicit setting of the delegate?