DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Marcos has posted 4 posts at DZone. View Full User Profile

Making The Methods And Properties Lookups Inside A Closure Be Resolved To The Delegate.

08.15.2008
| 2614 views |
  • submit to reddit
        // The following code shows how methods and properties will be resolved to the delegate instead of the closure itself.

// creating a closure
def closure = {
    add "groovy"
    add "java"
}

// declaring a list
def list = []

// making the call methods inside closure be resolved to the list
closure.delegate = list
closure.resolveStrategy = Closure.DELEGATE_FIRST

// executing the closure
closure();

// now, the list was changed
list.each { println it }

// output 
// groovy
// java