Looks like when you pass your List into a method/closure Groovy creates a copy of your List within the closure or method and updates that so your changes are not reflected once you exit!!! This is not the case if you use .add method
Why would Groovy want to change this most intuitive of behaviour for a Java developer when you use addition with Lists with the operator overloaded + sign ?
What would be the benefit from Groovy's point of view ?
If I were to uncomment the enhanced for loops in my code sample and comment out the addition of Lists using the + sign - it behaves as expected
*
List<String> b = ['man', 'lady']
def x = { List<String> p ->
println delegate.getClass().getName()
List<String> inx = ['dog','cat']
/*for (String l : inx)
{
p.add(l)
}*/
p = p + inx //value of p is not reflected whence you exit
}
def xmethod(List<String> p)
{
List<String> inx = ['monkey','lion']
/*for (String l : inx)
{
p.add(l)
}*/
p = p + inx // value of p is not reflected whence you exit
}
x( b )
println b
xmethod( b )
println b