简体   繁体   中英

Returning a method from closure in groovy

Since closures behave like inline methods (I guess, technically closures are compiled to classes), I didnt find how to return a method from closure in Groovy.

For example calling below method from main() should have printed only 1 if I did not used closure, but with closure it prints all 1, 2, 3:

public static returnFromClosure()
{
    [1,2,3].each{
        println it
        if (it == 1)
            return            
    }
}

How I can achieve this behavior?

Edit

The question was closed as a duplicate. My question was about closure in general. And I gave example of each{} loop which involves closure. I know the question about using break and continue in each{} loop are there, but that will deal about breaking that loop or continuing to the next iteration of the loop, but not about returning the calling code. The culprit behind this mis-understanding seems to be the example I gave above. However using return inside any loop is different from using break . I better give different example. Here it is, with plain closure:

static main(def args)
{
    def closure1 = { 
                      println 'hello';
                      return; //this only returns this closure, 
                              //not the calling function, 
                              //I was thinking if I can make it 
                              //to exit the program itself
                      println 'this wont print' 
                   }
    closure1();
    println 'this will also print :('
}

I'm not familiar with groovy but this seems to be expected behavior. Usually in an each statement it's literally running that one function seperately for each value in the array.

So you run it the first time the value is one, it passes the if statement and then returns.

Then next function runs and this time the value is two. It prints out 2, the value does not pass the if statement and then returns because its the end of the function.

If you want to only print values that match one you would do it like this.

public static returnFromClosure()
    {
        [1,2,3].each{

        if (it == 1)
            println it          
    }
}

If you want to stop executing the each function and move on after finding a value that equals one you should check out this other post. is it possible to break out of closure in groovy

Edit based on your update:

There is no specific mechanism that I'm aware of that works like you are saying. Closures are just function written in a specific context and just like any other function only return from their own execution. I think what you are wanting is something like this.

static main(def args){
    done = false;

    def closure1 = { 
                      println 'hello';
                      done = true;
                      return; //this only returns this closure, 
                              //not the calling function, 
                              //I was thinking if I can make it 
                              //to exit the program itself
                      println 'this wont print' 
                   }
    closure1();

    if( done ){
        return;
    }

    // this will no longer print =)
    println 'this will also print :('
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM