Also it gives more flexibility in terms of DSLs people can write. You can create your own control structures that look like built-in ones.
For instance imagine you want to have an "unless" pseudo-statement, you could write it like that: void unless(boolean b, Closure body) { if (!b) body() } and be able to call it like an "if" statement: unless(condition) { // do something } which is indeed equivalent to: unless(condition, { /* do something */ }) Guillaume On Tue, Dec 1, 2015 at 5:23 PM, Søren Berg Glasius <soe...@glasius.dk> wrote: > Hi Alessio, > > It's not a problem to use the closure like this: > > sql.eachRow('select * from PROJECT where name=:foo', [foo:'Gradle'], { > // process row > }) > > it is simply a matter of how you place your parentesees > > When you use closures with collections you would offen omit the > parentheses all together: > > collection.each { > // do something with it > } > > but writing > > collection.each ({ > // do something with it > }) > > yields the same result, but is a tiny bit more verbose > > Best regards / Med venlig hilsen, > Søren Berg Glasius > > Hedevej 1, Gl. Rye, 8680 Ry, Denmark > Mobile: +45 40 44 91 88, Skype: sbglasius > --- Press ESC once to quit - twice to save the changes. > > On December 1, 2015 at 17:19:07, alessio (aless...@gmail.com ) wrote: > > Hi, > > I have just started to dig into Groovy and have been mostly impressed > by what it offers, however when I had a look at handling database > calls (http://docs.groovy-lang.org/latest/html/api/groovy/sql/Sql.html) > I stumbled upon a rather weird syntax > > sql.eachRow('select * from PROJECT where name=:foo', [foo:'Gradle']) { > // process row > } > > Usually I'd say this is a "regular" block scope but in this context it > was obviously a closure/function pointer/callback. It took me a while > to dig into this to find out that it can also be passed as second > argument to the method and was added in 2.2 > > > http://www.groovy-lang.org/semantics.html#_calling_a_method_accepting_a_sam_type_with_a_closure > https://issues.apache.org/jira/browse/GROOVY-6188 (the JSR link does > not work anymore) > > My question now is, why? Apparently pre-2.2 one had to cast the > closure to the appropriate type. But why making it possible to > actually define the closure outside of the respective call? > > At least for me this is HIGHLY confusing. > > Thanks > > -- Guillaume Laforge Apache Groovy committer & PMC member Product Ninja & Advocate at Restlet <http://restlet.com> Blog: http://glaforge.appspot.com/ Social: @glaforge <http://twitter.com/glaforge> / Google+ <https://plus.google.com/u/0/114130972232398734985/posts>