# Deep copy a \`mutable struct\` defined with an inner constructor

**URL:** <https://discourse.julialang.org/t/deep-copy-a-mutable-struct-defined-with-an-inner-constructor/20954>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 18, 2019, 10:30pm UTC](https://discourse.julialang.org/t/deep-copy-a-mutable-struct-defined-with-an-inner-constructor/20954 "2019-02-18T22:30:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [February 18, 2019, 10:30pm UTC](https://discourse.julialang.org/t/deep-copy-a-mutable-struct-defined-with-an-inner-constructor/20954/1 "2019-02-18T22:30:55Z")

</div>

I’m in Julia 1.0.3, and I have a `mutable struct`

```julia
mutable struct MyStruct
    ID::Int64
    VAL::Float64
    function MyStruct(ID)
        b = new(ID)
        b.VAL = 0.0
    end
end

```

defined through an inner constructor. I want to deep-copy `ms_A = MyStruct(1)` into `ms_B` so that `ms_B` has the same values and fields as `ms_A`. However, my deep-copy function

```julia
Copy(x::T) where T = T([deepcopy(getfield(x, k)) for k ∈ fieldnames(T)]...)

```

can’t handle the inner-constructor. I’ve tried also adding an outer-constructor after my definition of `MyStruct` above

```julia
function MyStruct(ID, VAL)
    return MyStruct(ID, VAL)
end

```

but am still unable to deep copy. What’s the best way to approach this? Is there a way to still have the convenience constructor of only specifying `ID` in some cases and getting the full `MyStruct` with default `VAL = 0` but also be able to copy each field?

Thanks!

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 18, 2019, 10:40pm UTC](https://discourse.julialang.org/t/deep-copy-a-mutable-struct-defined-with-an-inner-constructor/20954/2 "2019-02-18T22:40:25Z")

</div>

The inner constructor is incorrect: it returns the value of `b.VAL` instead of the new object. You probably want something like this:

```julia
mutable struct MyStruct
    ID::Int64
    VAL::Float64
    function MyStruct(ID)
        b = new(ID, 0.0)
    end
end

```

In which case you can deep-copy an instance like this:

```julia
julia> ms_A = MyStruct(1)
MyStruct(1, 0.0)

julia> ms_B = deepcopy(ms_A)
MyStruct(1, 0.0)

julia> ms_A.ID = 42;

julia> ms_A
MyStruct(42, 0.0)

julia> ms_B
MyStruct(1, 0.0)

```

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [February 18, 2019, 10:41pm UTC](https://discourse.julialang.org/t/deep-copy-a-mutable-struct-defined-with-an-inner-constructor/20954/3 "2019-02-18T22:41:30Z")

</div>

Thanks!
