Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h2>What is a Closure?</h2> <p>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. It has some special properties like implicit variables, support for currying and support for free variables (which we'll see later on). We'll ignore the nitty gritty details for now (see the formal definition if you want those) and look at some simple examples.</p> <h4>Simple Example</h4> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def clos = { println "hello!" } println "Executing the Closure:" clos() //prints "hello!" </pre></td></tr></table> <p>Note that in the above example, "hello!" is printed when the Closure is <em>called</em>, not when it is defined.</p> <h2>Parameters</h2> <p>Closure parameters are listed before the <code>-></code> token, like so:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def printSum = { a, b -> print a+b } printSum( 5, 7 ) //prints "12" </pre></td></tr></table> <p>The <code>-></code> token is optional and may be omitted if your Closure definition takes fewer than two parameters.</p> <h4>Parameter notes</h4> <p>A Closure without -> , i.e. {} , is a Closure with one argument that is implicitly named as 'it'. (see below for details) In some cases, you need to construct a Closure with zero arguments, e.g. using <a class="confluence-link" href="/display/GROOVY/Strings+and+GString" data-linked-resource-id="2771" data-linked-resource-type="page" data-linked-resource-default-alias="Strings and GString" data-base-url="http://docs.codehaus.org">GString for templating</a>, defining <a class="confluence-link" href="/display/GROOVY/ExpandoMetaClass+-+Properties" data-linked-resource-id="79523" data-linked-resource-type="page" data-linked-resource-default-alias="ExpandoMetaClass - Properties" data-base-url="http://docs.codehaus.org">EMC Property</a> etc. You have to explicity define your Closure as { -> } instead of just { }</p> <p>You can also use varargs as parameters, refer to the <a class="confluence-link" href="/display/GROOVY/Closures+-+Formal+Definition" data-linked-resource-id="11403384" data-linked-resource-type="page" data-linked-resource-default-alias="Closures - Formal Definition" data-base-url="http://docs.codehaus.org">Formal Guide</a> for details. A JavaScript-style dynamic args could be simulated, refer to the <a class="confluence-link" href="/display/GROOVY/Closures+-+Informal+Guide" data-linked-resource-id="2729" data-linked-resource-type="page" data-linked-resource-default-alias="Closures - Informal Guide" data-base-url="http://docs.codehaus.org">Informal Guide</a>.</p> <h2>Free variables</h2> <p>Closures may refer to variables not listed in their parameter list. Such variables are referred to as "free" variables. They are "bound" to variables within the scope where they are defined:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def myConst = 5 def incByConst = { num -> num + myConst } println incByConst(10) // => 15 </pre></td></tr></table> <p>Or another example:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 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 </pre></td></tr></table> <h2>Implicit variables</h2> <p>Within a Groovy Closure, several variables are defined that have special meaning:</p> <h4>it</h4> <p>If you have a Closure that takes a single argument, you may omit the parameter definition of the Closure, like so:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def clos = { print it } clos( "hi there" ) //prints "hi there" </pre></td></tr></table> <h4>this, owner, and delegate</h4> <p><strong>this</strong> : as in Java, <code>this</code> refers to the instance of the enclosing class where a Closure is defined<br /> <strong>owner</strong> : the enclosing object (<code>this</code> or a surrounding Closure)<br /> <strong>delegate</strong> : by default the same as <code>owner</code>, but changeable for example in a <a class="confluence-link" href="/display/GROOVY/Builders" data-linked-resource-id="16958" data-linked-resource-type="page" data-linked-resource-default-alias="Builders" data-base-url="http://docs.codehaus.org">builder</a> or <a class="confluence-link" href="/display/GROOVY/ExpandoMetaClass" data-linked-resource-id="79517" data-linked-resource-type="page" data-linked-resource-default-alias="ExpandoMetaClass" data-base-url="http://docs.codehaus.org">ExpandoMetaClass</a></p> <p>Example:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 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 */ </pre></td></tr></table> <h2>Closures as Method Arguments</h2> <p>When a method takes a Closure as the last parameter, you can define the Closure inline, like so:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def list = ['a','b','c','d'] def newList = [] list.collect( newList ) { it.toUpperCase() } println newList // ["A", "B", "C", "D"] </pre></td></tr></table> <p>In the above example, the <code><a href="http://groovy.codehaus.org/groovy-jdk.html#meth223">collect </a></code> method accepts a <code>List</code> and a <code>Closure</code> argument. The same could be accomplished like so (although it is more verbose):</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def list = ['a','b','c','d'] def newList = [] def clos = { it.toUpperCase() } list.collect( newList, clos ) assert newList == ["A", "B", "C", "D"] </pre></td></tr></table> <h2>More Information</h2> <p>Groovy extends <code>java.lang.Object</code> and many of the <code>Collection</code> and <code>Map</code> classes with a number of methods that accept Closures as arguments. See <a class="confluence-link" href="/display/GROOVY/GDK+Extensions+to+Object" data-linked-resource-id="11403319" data-linked-resource-type="page" data-linked-resource-default-alias="GDK Extensions to Object" data-base-url="http://docs.codehaus.org">GDK Extensions to Object</a> for practical uses of Groovy's Closures.</p> <p>See Also:</p> <img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2NoaWxkcmVufQ&locale=en_GB&version=2" data-macro-name="children">
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced