Why doesn't modifying a shallow copy of an array reflect on the original?

Suppose if I had the following array:

even_numbers = Int32[2,4,6,8,10] # Creating a 5 element array.

and I made a shallow copy:

b = copy(even_numbers)

If I were to modify or delete an element from b why doesn’t it reflect on even_numbers?

deleteat!(b, 2) # the element of 4 should be removed.

If I were to display even_numbers, 4 still exists.

5-element Array{Int32,1}:
  2
  4
  6
  8
 10

I always thought if you modify a shallow copy of a collection it will also modify the original collection. If you didn’t want to modify the original, you can use deepcopy()

1 Like

Because even a shallow copy has to mean something other than just adding a reference to the same object. The behavior you expect you would get from doing

b = even_numbers

This is a better example of a shallow copy:

julia> a = [[1, 2], [3, 4]]
2-element Array{Array{Int64,1},1}:
 [1, 2]
 [3, 4]

julia> b = copy(a)
2-element Array{Array{Int64,1},1}:
 [1, 2]
 [3, 4]

julia> push!(b[1], 5)
3-element Array{Int64,1}:
 1
 2
 5

julia> a
2-element Array{Array{Int64,1},1}:
 [1, 2, 5]
 [3, 4]

julia> b
2-element Array{Array{Int64,1},1}:
 [1, 2, 5]
 [3, 4]
5 Likes

You may understand shallow as “just one level” and deep as “all levels”.

First let us have a vector of vectors, a shallow copy of it, and a deep copy of it.

julia> a1, a2, a3 = [1, 2, 3], [4, 5, 6], [7, 8, 9]
([1, 2, 3], [4, 5, 6], [7, 8, 9])

julia> a123 = [a1, a2, a3]
3-element Array{Array{Int64,1},1}:
 [1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]

julia> shallow = copy(a123);

julia> deep = deepcopy(a123);

Now, if we mutate the first element inside the original vector (a123), this reflects in the shallow but not in the deep.

julia> a123[1][1] = 10
10

julia> shallow[1]
3-element Array{Int64,1}:
 10
  2
  3

julia> deep[1]
3-element Array{Int64,1}:
 1
 2
 3

The reason is that shallow has allocated a new vector with the same three inner vectors, i.e., it copied just the first level that is the external vector, but did not copy the second level that is each element. shallow has the same elements as a123 inside it. The deepcopy has created a copy of each element thus remain unaffected.

To check if the copy has really copied the first level (i.e., the outer vector), we can change the outer vector (instead of the elements inside it) and see the changes do not reflect in the original.

julia> shallow[1] = [30, 20, 10];

julia> shallow
3-element Array{Array{Int64,1},1}:
 [30, 20, 10]
 [4, 5, 6]
 [7, 8, 9]

julia> a123
3-element Array{Array{Int64,1},1}:
 [10, 2, 3]
 [4, 5, 6]
 [7, 8, 9]
1 Like