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
Making The Methods And Properties Lookups Inside A Closure Be Resolved To The Delegate.
// 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





