I think the title speaks for itself - does Groovy have something like Kotlin scope functions?
obj.apply {
foo()
bar()
baz()
}
// is the same as
obj.foo()
obj.bar()
obj.baz()
CodePudding user response:
Groovy has obj.with { }
method that allows you to do the same:
obj.with {
foo()
bar()
baz()
}
There is also obj.tap { }
variant (an equivalent of obj.with(true) { }
) that does the same, but it returns the incoming object.
def newObj = obj.tap {
foo()
bar()
baz()
}