> When a shallow sequence is resized, only the variable currently being
> modified has its reference updated; the other variables will still have
> references to the old data.
proc foo() =
var a = newSeqOfCap[int](2)
a.add(1)
var b: seq[int]
shallowCopy(b, a)
a[0] = 0 # modify sequence after copying
a.add(2) # further modification
echo a
echo b
b.add(3) # resizing happening here!
echo a
echo b
foo()
This outputs:
@[0, 2]
@[0, 2]
@[0, 2, 3]
@[0, 2, 3]
Modifications after shallow copying are fine because strings and seqs are
garbage-collected heap-objects. As you see, both references are still fine
after resizing happened.