Wednesday, August 29, 2012

Groovy's each does not exit when using return

Groovy's each is a very useful closure in collections. However, if you want to exit out of the code using return, it does not work. Below is the code. In this case, if the listId is empty, it sets the value for the "isValid" variable but never returns or break from the loop. The way that I was able to solve it was using a "find" method. Inside the if statement, I set the value for the "isValid" and then return true. The "true" value tells the "find" statement that the value was found, and therefore, it exits out.

1 comment:

  1. Thanks!
    I hit the same problem.
    Didn't know about .find.
    I do now :-)