So from what I understand when you pass the message copy
to an array, a shallow copy is performed. Two objects should point to the same array and in theory, when you change one, you change the other. However, when I run this code, it acts like it's a deep copy. Each array is independent from each other. I make changes in the first array and the second array is unaffected. I make changes in the second array and the first array is unaffected. So how is copying actually working when I use copy
?
| a b |
a := #('first' 'second' 'third').
b := a copy.
Transcript show: a = b;cr.
Transcript show: a == b;cr.
Transcript show: (a at: 1) == (b at: 1);cr.
b at: 1 put: 'newFirst'.
Transcript show: a;cr.
Transcript show: b;cr.
Transcript show: (a at: 1) == (b at: 1);cr.
a at: 2 put: '2nd'.
Transcript show: a;cr.
Transcript show: b;cr.
a := nil.
Transcript show: a;cr.
Transcript show: b;cr.
The results are:
true
false
true
#('first' 'second' 'third')
#('newFirst' 'second' 'third')
false
#('first' '2nd' 'third')
#('newFirst' 'second' 'third')
nil
#('newFirst' 'second' 'third')