deepcopy
deepcopy(x)
Create a deep copy of x
: everything is copied recursively, resulting in a fully independent object. For example, deep-copying an array produces a new array whose elements are deep copies of the original elements. Calling deepcopy
on an object should generally have the same effect as serializing and then deserializing it.
As a special case, functions can only be actually deep-copied if they are anonymous, otherwise they are just copied. The difference is only relevant in the case of closures, i.e. functions which may contain hidden internal references.
While it isn't normally necessary, user-defined types can override the default deepcopy
behavior by defining a specialized version of the function deepcopy_internal(x::T, dict::ObjectIdDict)
(which shouldn't otherwise be used), where T
is the type to be specialized for, and dict
keeps track of objects copied so far within the recursion. Within the definition, deepcopy_internal
should be used in place of deepcopy
, and the dict
variable should be updated as appropriate before returning.
Examples
julia> foo = [1,2,3];
julia> dict = {"a"=>foo};
julia> bar = copy(dict)
Dict{Any,Any} with 1 entry:
"a" => [1,2,3]
julia> baz = deepcopy(dict)
Dict{Any,Any} with 1 entry:
"a" => [1,2,3]
julia> foo[1] = 42;
julia> bar
Dict{Any,Any} with 1 entry:
"a" => [42,2,3]
julia> baz
Dict{Any,Any} with 1 entry:
"a" => [1,2,3]
julia> arr = [1, 2, [3, 4]];
julia> new_arr = deepcopy(arr);
julia> arr[3][1] = 5;
julia> arr
3-element Array{Any,1}:
1
2
[5, 4]
julia> new_arr
3-element Array{Any,1}:
1
2
[3, 4]
In this example, deepcopy
is used to create a deep copy of the array arr
. Modifying an element of the original array (arr[3][1] = 5
) does not affect the corresponding element in the deep-copied array (new_arr
). The deep copy ensures that changes made to one object do not affect the other.
julia> dict = Dict("a" => [1, 2, 3], "b" => [4, 5, 6]);
julia> new_dict = deepcopy(dict);
julia> dict["a"][2] = 7;
julia> dict
Dict{String,Array{Int64,1}} with 2 entries:
"b" => [4, 5, 6]
"a" => [1, 7, 3]
julia> new_dict
Dict{String,Array{Int64,1}} with 2 entries:
"b" => [4, 5, 6]
"a" => [1, 2, 3]
Here, deepcopy
is used to create a deep copy of a dictionary dict
. Modifying the value of a key in the original dictionary does not affect the corresponding value in the deep-copied dictionary.
julia> function add_one(x)
return x + 1
end
julia> new_func = deepcopy(add_one);
julia> add_one(5)
6
julia> new_func(5)
6
In this example, deepcopy
is used to copy a function add_one
. The deep copy allows the copied function new_func
to produce the same output as the original function when called.
julia> struct CustomType
data
end
julia> obj = CustomType([1, 2, 3]);
julia> new_obj = deepcopy(obj);
julia> obj.data[2] = 4;
julia> obj.data
3-element Array{Int64,1}:
1
4
3
julia> new_obj.data
3-element Array{Int64,1}:
1
2
3
In this example, deepcopy
is used with a user-defined type CustomType
. The deep copy ensures that modifying the data field of the original object does not affect the corresponding field in the deep-copied object.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.