Groovy web console

subscribe to the feed Subscribe
to this
site
Test (via #groovywebconsole)
tweet this snippet Tweet
this
script

Test

Published 3 months ago by Test with tags groovy closures
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
//http://mrhaki.blogspot.in/2009/11/groovy-goodness-setting-closures.html//http://www.tothenew.com/blog/closure-delegate-using-groovy-with-method-and-decorating-code-with-multiple-assignments/
class Post {
    int count
    def info() { "This is Groovy Goodness post #$count!" }
}

count = 0
def info() {
    "Count value is $count."
}

def printInfo = {
    count++
    info()
}
 
assert "Count value is 1." == printInfo() // Delegate is by default set to owner, so the script in this case.
 
printInfo.resolveStrategy = Closure.DELEGATE_FIRST  // Change closure resolver so first the delegate is used.
printInfo.delegate = new Post(count: 100)  // Set delegate to Post object.
assert "This is Groovy Goodness post #101!" == printInfo()