Groovy Notes

  • Allow more ways of creating objects using literals

  • Introduce new datatypes together with their operators and expressions.

Closure

simple abbreviated syntax of closures: after a method call, put code in braces with parameters delimited from the closure body by an arrow.

log = ''
(1..10).each{ log += it }
assert log == '12345678910'


log = ''
(1..10).each{ counter -> log += counter }
assert log == '12345678910'

A second way of declaring a closure is to directly assign it to a variable:

def printer = { line -> println line }
  • a single parameter default name(it)

Similarly, if the closure needs to take only a single parameter to work on, Groovy provides a default name—it—so that you don’t need to declare it specifically

  • Closures are Groovy’s way of providing transparent callback targets as first-class citizens.

Related