Groovy web console

subscribe to the feed Subscribe
to this
site

Delegate

Published 2 weeks ago by Victor Balakine with tags closure delegate map
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
class Person {
String name
String age
def pretty = { "My name is $name, I'm $age" }
}
class Thing {
String name
}
def p = new Person(name: 'Sarah', age: '1')
def t = [name: 'Teapot'] //as Thing
println p.pretty()
p.pretty.delegate = t
println p.pretty()
p.pretty.resolveStrategy = Closure.DELEGATE_FIRST
println p.pretty()