Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Closures and Groovy

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Closures in Groovy
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic