Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

·
Free Resource
// 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
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}