Skip to: Site menu | Main content

Groovy 

      Download | Documentation | Developers | Community

An agile dynamic language for the Java Platform

Closures Add comment to Wiki View in Wiki Edit Wiki page Printable Version

What is a Closure?

A Groovy closure is like a "code block" or a method pointer. It is a piece of code that is defined and then executed at a later point.

Simple Example

def clos = { println "hello!" }

println "Executing the closure:"
clos()                          //prints "hello!"

Note that in the above example, "hello!" is printed when the closure is called, not when it is defined.

Closures may be "bound" to variables within the scope where they are defined:

def localMethod() {
  def localVariable = new java.util.Date()
  return { println localVariable }
}

def clos = localMethod()

println "Executing the closure:"
clos()                          //prints the date when "localVariable" was defined

Parameters

Closure parameters are listed before the -> token, like so:

def clos = { a, b -> print a+b }
clos( 5, 7 )                       //prints "12"

The -> token is optional and may be omitted if your closure definition takes fewer than two parameters.

Implicit variables

Within a closure, several variables are defined that have special meaning:

It

If you have a closure that takes a single argument, you may omit the parameter definition of the closure, like so:

def clos = { print it }
clos( "hi there" )              //prints "hi there"

this, owner, and delegate

this : as in Java, this refers to the enclosing class where a Closure is defined

owner : the enclosing object (this or a surrounding closure)

delegate : by default the same as owner, but changeable for example in a builder or ExpandoMetaClass

Example:

class Class1 {
  def closure = {
    println this.class.name
    println delegate.class.name
    def nestedClos = {
      println owner.class.name
    }
    nestedClos()
  }
}

def clos = new Class1().closure
clos.delegate = this
clos()
/*  prints:
 Class1
 Script1
 Class1$_closure1  */

Closures as Method Arguments

When a method takes a closure as the last parameter, you can define the closure inline, like so:

def list = ['a','b','c','d']
def newList = []

list.collect( newList ) {
  it.toUpperCase()
}
println newList           //  ["A", "B", "C", "D"]

In the above example, the collect method accepts a List and a Closure argument. The same could be accomplished like so (although it is more verbose):

def list = ['a','b','c','d']
def newList = []

def clos = { it.toUpperCase() }
list.collect( newList, clos )

assert newList == ["A", "B", "C", "D"]

More Information

Groovy extends java.lang.Object with a number of methods that accept closures as arguments. See GDK Extensions to Object for practical uses of closures.

See Also: