Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy - closures vs methods - the difference

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?

like image 436
Alexander Mills Avatar asked Feb 20 '14 10:02

Alexander Mills


People also ask

What are closures in Groovy?

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.

What is a method in Groovy?

A method is in Groovy is defined with a return type or with the def keyword. Methods can receive any number of arguments. It's not necessary that the types are explicitly defined when defining the arguments. Modifiers such as public, private and protected can be added.

What is call method in Groovy?

In Groovy we can add a method named call to a class and then invoke the method without using the name call . We would simply just type the parentheses and optional arguments on an object instance. Groovy calls this the call operator: () . This can be especially useful in for example a DSL written with Groovy.

What is the role of closure and listeners in Groovy?

It references the variables which are declared in its surrounding scope. We know that anonymous inner classes are not supported by Groovy. Inline listeners can be determined with the help of closures. In groovy, listener closures are used as listener adapters.


1 Answers

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.

like image 132
Seagull Avatar answered Sep 20 '22 11:09

Seagull