Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>12/05/2013 Heavily Edited.</em></p> <p><strong>Answering the question that was asked.</strong></p> <blockquote> <p>Is it possible to break out of a Closure?</p> </blockquote> <p>You would <em>"break"</em> out of a closure by issuing the <code>return</code> keyword. However that isn't helpful in the example that is given. The reason for this is that the closure (think of it as a method) is called by the <code>each</code> method for every item in the collection.</p> <p>If you run this example you will see it will print 1 then 3.</p> <pre><code>[1, 2, 3].each { if (it == 2) return println(it) } </code></pre> <p><strong>Why <code>break</code> in the context of <code>each</code> doesn't make sense.</strong></p> <p>To understand why you cannot break out of the <code>each</code> method like you could break out of a <code>for</code> loop you need to understand a bit of what is actually happening. Here is a gross simplification what the each method on a collection does.</p> <pre><code>myEach([0,1,3]) void myEach(List things) { for (i in things) { myEachMethod(i) } } void myEachMethod(Object it) { // this is your Closure if(it == 2) return println it } </code></pre> <p>As you can see the closure is basically a method that can be passed around. Just as in java you cannot break from within method call or closure.</p> <p><strong>What to do instead of breaking from <code>each</code>.</strong></p> <p>In Groovy you are supposed to express your code using high level abstractions as such primitive looping is not idiomatic. For the example that you gave I would consider making use of findAll. For example:</p> <pre><code>[1,2,3].findAll { it &lt; 2 }.each { println it } </code></pre> <p>I hope this helps you understand what is going on.</p> <p><strong>Answering the implied question.</strong></p> <blockquote> <p>Can you break out of the <code>Collection.each</code> iterations against your supplied closure?</p> </blockquote> <p>You cannot break out of the <code>each</code> method without throwing and catching an exception as John Wagenleitner has said. Although I would argue that throwing and catching an exception in the name of flow control is a code smell and a fellow programmer might slap your hands.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload