Created
June 28, 2013 18:15
-
-
Save radeinla/5886802 to your computer and use it in GitHub Desktop.
Newbie at this but why is this printing only the last value of the for loop and not binding each value of the list in the for loop? My expectation was for the last closure statement:
obj1
obj2
obj3 but what is actually happening is:
obj3
obj3
obj3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Integer> list = [1, 2, 3].collect { new Object() } | |
list.each { println it } | |
List<Closure> cls = [] | |
for (Object i : list) { | |
cls << { -> | |
println i | |
} | |
} | |
cls.each { | |
it() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You probably wanted this instead:
Although the second part can be better written as