Created
November 27, 2013 16:07
-
-
Save ivanarrizabalaga/7678235 to your computer and use it in GitHub Desktop.
Appending elements to a groovy list brings a hidden problem inside due to overriden operators. output+=item
output.add(item) Is not always the same.
This file contains 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
def input=[1,[2,3],4] | |
def output=[] | |
//Warning!! Potential error | |
input.each{ | |
output+=it | |
} | |
println output //=>[1,2,3,4]!!! | |
println output.size()//=>4 | |
//Right way | |
output=[] | |
input.each{ | |
output.add(it) | |
} | |
println output //=>[1,[2,3],4]!!! | |
println output.size()//=>3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment