Yes, there is a better idea.
Refer Expando instance directly (instead of delegate) when you know you are having a closure(dynamic method) defined for the Expando
which is nothing but a dynamic bean. On the other hand, the same test would yield expected result when tested against a concrete class:
def e = new Expando()
e.val = 'hi'
e.doit = {
println delegate.class.name
"$e.val Joseph"
}
assert e.doit() == 'hi Joseph'
assert e.doit.call() == 'hi Joseph'
class Test{
String a
def printA = {
println delegate.class.name
"$delegate.a Joseph"
}
}
def test = new Test(a: 'hi')
assert test.printA() == 'hi Joseph'
assert test.printA.call() == 'hi Joseph'
Note system out from println
in both cases.