Book Image

Mastering Gradle

Book Image

Mastering Gradle

Overview of this book

Table of Contents (17 chapters)
Mastering Gradle
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Closure


Closure has usually been associated with functional languages. Groovy provides a very easy way of creating closure objects. A Groovy Closure is like a code block written in curly braces. Many people associate Closure to be an anonymous function in Java.

Closure in Groovy may accept arguments and returns a value. By default, the last statement in a Groovy Closure is the return statement. It means that if you are not explicitly returning any value from Closure, it will by default, returns the output of the last statement of Closure. Commonly, we define a Closure like this {argument list-> closure body}. Here, an argument list is a comma separated value that Closure accepts. Arguments are optional. If no argument is specified, then one implicit untyped argument named it will be available in the Closure body. The argument it will be null if no argument is supplied during Closure invocation.

In the following example, for the first call of Closure addTwo the variable it is assigned is...