Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Closures and Groovy

 
Trevor Butler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott,

Hello again - it's Trevor Butler from SAS. I enjoyed your last class here on Java 5, and at the end, you brought up Groovy and gave us an introduction.

I recently had the need to perform a search and replace on a text file to automate some testing I am doing, so I loaded up the Groovy jar and got to work. In combing through the codehaus site and a Groovy book a colleague had, I was fascinated by closures. Coming from Java, most of the Groovy language made sense either as a simpler syntax or shorthand. However, closures were different.

I have never used any other language that had closures, and it took me a while to get the hang of them. I still am not entirely comfortable with them beyond basic for-each type looping scenarios.

Since I found closures to require a paradigm shift in thinking, I am guessing other folks coming from Java and other static OO languages might feel the same.

Question: How would you describe closures to a person such as myself who comes from a Java / static OO / procedural language world in such a way that they can grasp the concept?

Thanks,
Trevor
 
S Davis
Author
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trevor -- good hearing from you again.

Closures are a bit of a conceptual leap for Java developers. (Bear in mind that closures are due to be added to Java 1.7, so thanks to Groovy you're getting a bit of a sneak preview now... grin)

The easiest way to think of a closure is a freestanding block of code that isn't attached to a class. Things that you might consider making a static method on a utility class can be declared as a closure instead. So rather than the anonymous blocks of code we're used to seeing surrounded in curly braces after if statements, while loops, try/catch blocks, etc, can now be stored in a named variable and called at will.

All metaprogramming that you do involves closures. If you want to add a new method to a class, you create a closure and add it to the classes ExpandoMetaClass.

All of the "methods" in a Grails controller are, in fact, closures. Simply named blocks of code.

Here are a few links that might help:
Informal Guide to Closures
Formal Guide to Closures
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Scott]: Bear in mind that closures are due to be added to Java 1.7

Hm, have you heard indications that that's definite? My impression is it's tentative - a lot of people are interested in getting some closure-like feature(s) into JDK 1.7, but there's still disagreement about how to do it, and also a fair amount of resistance to the idea. Seems to me like there's a fair chance this might get pushed to later, or possibly dropped entirely. I hope not, but I don't know.
 
Krishna Srinivasan
Ranch Hand
Posts: 1874
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Closures in Groovy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic