In the last example of this page (http://groovy.codehaus.org/JN3525-MetaClasses), the closure code to override the metaClass invokeMethod call refers to a "delegate". Code is also copied below:
class Bird{
def name= 'Tweety'
def twirp(){ 'i taught i saw a puddy cat' }
}
Bird.metaClass.invokeMethod= {name, args->
def metaMethod= Bird.metaClass.getMetaMethod(name, args)
//'getMetaMethod' gets method, which may be an added or an existing one
metaMethod? metaMethod.invoke(delegate,args): 'no such method'
}
def a= new Bird()
assert a.twirp() == 'i taught i saw a puddy cat'
assert a.bleet() == 'no such method'
Bird.metaClass.getProperty= {name->
def metaProperty= Bird.metaClass.getMetaProperty(name)
//'getMetaProperty' gets property, which may be an added or an existing one
metaProperty? metaProperty.getProperty(delegate): 'no such property'
}
def b= new Bird()
assert b.name == 'Tweety'
assert b.filling == 'no such property'
Where exactly is the delegate coming from and two what does it refer to?
It seems to refer to the Bird class, but how does this fit into Groovy closures and the implementation of the language as a whole?