Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 7

[ 161 ]

The following result is a quick and nasty UI for Twitter searching:


Method pointers


Groovy allows you to assign a method to a closure by using the & syntax. The closure
returned is often referred to as a method pointer. Method pointers can be assigned
by dereferencing the method name from any object instance, for example:


given:
def list = ["A", "B", "C"]

when:
def addToList = list.&add

and:
addToList "D"

then:
list == ["A", "B", "C", "D"]
Free download pdf