vendredi 7 août 2015

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?

Aucun commentaire:

Enregistrer un commentaire