Closures

A closure is a function that you can pass as an argument to another function.

It is contained by curly brackets, {<closure>}.

You can also define variables before the arrow ->

Tip For more extensive documentation about closures, please refer to the Groovy closure documentation.

Examples

Closure Purpose
{a, b -> a + b}
  1. Define two variables of an undefined type.
  2. Add them up.
  3. Show the result.
{String a -> isNotEmpty (a) }
  1. Define a variable of the type String.
  2. Apply the validation function isNotEmpty on it.
    More info: isNotEmpty (value).
{isNotEmpty (it)}
  1. Apply the validation function isNotEmpty on any variable that has it as its name.
    More info: isNotEmpty (value).

Tip Naming an argument is not necessary when there is only one.