Dhanush Gopinath
Dhanush Gopinath

Reputation: 5739

Groovy each method/closure

What is the difference between using

teams.each(){ team->
//iterate through each team
}

and

teams.each { team->
//iterate through each team
}

Please note in the first one there is a bracket after each and before the closure. We were doing some load testing on our application and noticed that when we use the first one, there are some threads waiting on at this call. But in the second case all works smoothly.

I am curious to know what is the difference here.

Upvotes: 1

Views: 1145

Answers (1)

tim_yates
tim_yates

Reputation: 171074

Nothing, they are the same thing. In Groovy, if the last parameter to a function is a Closure, then it can occur outside the braces, so for example:

[1,2,3].inject( 0 ) { acc, it -> acc + it }

and

[1,2,3].inject( 0, { acc, it-> acc + it } )

Are the same thing. The usual way of writing groovy is to miss out the braces in your example, or put the closure outside the braces in the inject example above

Upvotes: 4

Related Questions