Closures : Groovy and Java


Blogs: Closures : Groovy and Java

  1. Closures : Groovy and Java (1 messages)

    What is it about closures that they've become such an important topic of discussion for future release of the Java language ? Joey Gibson writes about his love for closures, providing various code listings written in Groovy and Ruby to illustrate just what the Java language and you yourself might be missing out on. Read Joey's post on 'Why I Love Closures'
  2. Re: Closures : Groovy and Java[ Go to top ]

    Well, when I hear about closures, I thought it was a big disadvantage for Java that this feature is missing in Java. But while time passes I change my mind. Closures are nothing more than a beautiful syntax for a method call with an anonymous class of an interface as a parameter. The first comment of the linked blog shows a good example. An other example I used, is a Generic-"Closure"-LineReader as a method. parseFileByLine(/*File*/ f, new LineHanlder() { private SpecialParserOrCacheOrWhatEver internalResource; public void parseLine(String line) throws IOException, ApplicationException { internalResource = new SpecialParserOrCacheOrWhatEver(); //... to some thing ... } public void close() thows IOException { if (null =! internalResource) { internalResource.close(); } } }; In this example with an anonymous class the closure method/class get an additional method for exception handling. The whole exception handling is done within the closure method/class. Use cases like this are very specific. Using this "closure" style looks good. It makes your code (divided in input stream/exception hanling code and business case code) better readable. And you enforce the DRY concept. (DRY = don't repeat yourself) Well, I don't think, I tell something new to you. But may be it is interesting to read. If you know this "closure" like style and lern to use it then the missing of the closure feature in java isn't a so big disadvantage.