0

The first return in the following method does not end the method, but instead breaks out of the loop

boolean isAnyColorRed() {
  colors.each { 
    if (it.type == "red")
       return true // does not end the method but instead breaks the loop.
  }
  return false
}

What is a workaround? This?

boolean isAnyColorRed() {
  boolean foundRed = false
  colors.each { 
    if (it.type == "red") {
        foundRed = true
        return //break the loop
    } 
  }
  if (foundRed)
    return true
  else
    return false
}
Anthony
  • 33,838
  • 42
  • 169
  • 278

1 Answers1

0

A much groovier way to do this:

colors.any { it.type == 'red' }

That will return true or false, so you can use it in an if expression...

Chris Roberts
  • 346
  • 3
  • 4