Creo que el título habla por sí mismo: ¿Groovy tiene algo así como las funciones de alcance de Kotlin?
obj.apply {
foo()
bar()
baz()
}
// is the same as
obj.foo()
obj.bar()
obj.baz()
Solución del problema
Groovy tiene obj.with { }
un método que te permite hacer lo mismo:
obj.with {
foo()
bar()
baz()
}
También existe una obj.tap { }
variante (un equivalente de obj.with(true) { }
) que hace lo mismo, pero devuelve el objeto entrante.
def newObj = obj.tap {
foo()
bar()
baz()
}
Fuente: http://docs.groovy-lang.org/docs/next/html/documentation/style-guide.html#_using_with_and_tap_for_repeated_operations_on_the_same_bean
No hay comentarios:
Publicar un comentario