Java Reference
In-Depth Information
the closure is invoked. If you think of the variable like a method parameter, you've got the
basic idea.
The closure can be invoked in one of two ways: either by using the reference as though
it's a method call, or by explicitly invoking the call method on it. Because the last value
computed by a closure is returned automatically, both ways return the argument to the clos-
ure, which is why it was called echo in the first place.
Closure Return Values
The last evaluated expression in a closure is returned automatically.
If a closure takes more than one argument, or if you don't want to use the default name,
use an arrow to separate the dummy argument names from the body of the closure. Here's
a simple sum, once with the default and once with a named argument:
def total = 0
(1..10). each { num -> total += num }
assert (1..10). sum () == total
total = 0
(1..10). each { total += it }
assert (1..10). sum () == total
Closures are used throughout this topic and fill an entire chapter in GinA . This little amount
of information is enough to make a lot of progress.
Returning to the basic constructs of the language, I'll now show how Groovy differs from
Java when using loops and conditional tests.
B.6. Loops and conditionals
In this section, I'll discuss two features that appear in any programming language: looping
through a set of values and making decisions.
Search WWH ::




Custom Search