Why does append! apply to both vectors

Dear Julia Community,

I struggle to understand the append!-function and I would much appreciate any help:) Consider the following minimal example

x_0  = [1;1]
x_1  = x_0
x_2  = x_0
append!(x_1,x_2)

As a result all three vectors have now a length of 4. However, the example

x_0  = [1;1]
x_1  = [1;1]
x_2  = x_0
append!(x_1,x_2)

creates the expected result: x_1 has length 4, while the other two have a length of 2.

Thank you very much for your help!

Assignment to an untyped variable never make a copy so x_0, x_1 and x_2 are all the same array (in the first case. Or x_2 and x_0 in the second case).

2 Likes

Thats fantastic. Thank you very much for your explanation. As a coding-novice, I find it surprising that arrays behave in such a way. Is there preferred way to create independent copies on an array? Currently, I multiply each one by 1 to get around.

copy