13

enter image description here

If you look very carefully at the picture included, you will notice that you can refactor Groovy code using the Eclipse IDE and convert a method to a closure and vice versa. So, what exactly is a closure again and how is it different than a method? Can someone give a good example of using a closure as well as why it's a useful feature? Anonymous inner classes weren't good enough?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

4 Answers4

16

Closure is a Closure class instance, that implements Call logic. It may be passed as argument or assigned to a variable. It also has some logic concerned with scope variable accessing and delegating calls.

Methods are normal Java methods. Nothing special.

And yes, anonymous inner classes have a lot of boilerplate code to perform simple actions.

Compare:

button.addActionListener(
  new ActionListener() {
     public void actionPerformed( ActionEvent e ) {
          frame.dispose();
     }
  }
);

vs

button.addActionListener { frame.dispose() }

There is a related question on SO Groovy : Closures or Methods and the following link(s) to the user guide containing a lot of useful information.

  1. http://groovy-lang.org/closures.html

A closure in Groovy is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable. A closure may reference variables declared in its surrounding scope. In opposition to the formal definition of a closure, Closure in the Groovy language can also contain free variables which are defined outside of its surrounding scope. While breaking the formal concept of a closure, it offers a variety of advantages which are described in this chapter.

Ashutosh Jindal
  • 18,501
  • 4
  • 62
  • 91
Seagull
  • 13,484
  • 2
  • 33
  • 45
  • See [some reasons why](http://www.javanicus.com/blog2/items/191-index.html) your example isn't a true lexical closure, and even Groovy's creator called it "fundamentally broken". – Vorg van Geir Feb 21 '14 at 12:54
  • @VorgvanGeir Many thanks for interesting link but I'm not sure that it is correlated to OP's question. – Seagull Feb 22 '14 at 16:00
  • @Seagull The codehaus links you have posted, are no longer available. Can you please update them with this: http://groovy-lang.org/closures.html . Thanks in advance. – Abhishek Oza Feb 27 '18 at 07:31
4

Also, as Closures are first class objects, they can be passed around, returned and manipulated. Consider:

def add = { n, m -> n + m }
def add2 = add.curry( 2 )

assert add2( 4 ) == 6

def makeAdder = { n ->
    // return a Closure
    { m -> n + m }
}
def anotherAdd2 = makeAdder( 2 )

assert anotherAdd2( 4 ) == 6
tim_yates
  • 167,322
  • 27
  • 342
  • 338
0

A closure in Groovy is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable. The following link containing a lot of useful information. http://www.groovy-lang.org/closures.html

0

I think Closure is closely relating to functional interface in Java that implements Call method. It's anonymous, can has as many as input needed, can return a data type.

Maybe it's useful in defining event handlers or listeners.