Difference between deepcopy() and deepcopy.() ? Do I have to broadcast deepcopy over lists?

I have seen a behaviour that confused me a little bit. I have a list with structs (list_of_structs) and I selected specific indices of it with an integer array (idx_array). When I then manipulate structs of the list_of_structs, which had the same index of the idx_array, I manipulate all of them. After that I used deepcopy, but this led to the same behaviour. Then I used the broadcast version of deepcopy, which works fine, but now I´m not sure when I have to use deepcopy() and when deepcopy.().

list_of_structs = deepcopy(list_of_strucs[idx_array])
list_of_structs = deepcopy.(list_of_strucs[idx_array])

In example I have a struct with multiple different data types in it, including a list of structs as in the example above. Do I have to use deepcopy() or deepcopy.() to make independent copy of my_struct1 ?

mutable struct MyStruct2
  var1::Array{Int64}
  var2::Float64
end

mutable struct MyStruct1
   list_of_structs::Array{MyStruct2}
   var1::Int64
   var2::DataFrame
end

If you want independent copies of vectors of MyStruct1s, which themselves have arrays of MyStruct2, which contains arrays, you should make deep copies.

I am assuming that the “list” here is a Vector. deepcopy should be fine since it will copy the vector itself deeply.

(incidentally, you may want to avoid abstract type parameters)

1 Like

Ok I have indeed an abstract type of Array{Array{Number}} in MyStruct2. So it would be better to store it in an Array{Array{Float64}} data type and then to cast it when I need it to be an Int64?

That depends on the context (which I don’t have), a type parameter may be enough.