O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Closure Delegation

Closures in Groovy go far beyond being simple anonymous methods; they have some powerful capabilities, as we’ll see in the rest of this chapter. Groovy closures support method delegation, and provide capabilities for method-dispatching—much like JavaScript’s support for prototypal inheritance. Let’s understand the magic behind this and how to put this to good use.

Three properties of a closure—this, owner, and delegate—determine which object handles a method call from within a closure. Generally, the delegate is set to owner, but changing it lets us exploit Groovy for some really good metaprogramming capabilities. Let’s examine these properties for closures:

UsingClosures/ThisOwnerDelegate.groovy
 
def​ examiningClosure(closure) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required