Passing Parameters to Closures

In the previous sections, we saw how to define and use closures. In this section, we’ll talk about how to send multiple parameters to closures.

it is the default name for a single parameter passed to a closure. We can use it as long as we know that only one parameter is passed in. If we have more than one parameter passed, we need to list those by name, as in this example:

UsingClosures/ClosureWithTwoParameters.groovy
 
def​ tellFortune(closure) {
 
closure ​new​ ​Date​(​"09/20/2012"​), ​"Your day is filled with ceremony"
 
}
 
tellFortune() { date, fortune ->
 
println ​"Fortune for ​${date}​ is '​${fortune}​'"
 
}

The method tellFortune calls its closure with two parameters, namely an instance of Date and a ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.