# How to copy all fields without changing the referece?

**URL:** <https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945>\
**Category:** General Usage\
**Created:** [December 14, 2016, 3:33pm UTC](https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945 "2016-12-14T15:33:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 14, 2016, 3:33pm UTC](https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945/1 "2016-12-14T15:33:27Z")

</div>

Hi guys!

I have a type with many fields and I need to define the function `copy!(A,B)`. My question is: is there any way to copy all the fields automatically? Something like `deepcopy` but without changing the reference?

As of now, I am doing it manually:

```julia
function copy!(A::MyType, B::MyType) = begin
    A.f1 = B.f1
    A.f2 = B.f2

    copy!(A.v1, B.v1)
    ...
end

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 14, 2016, 3:47pm UTC](https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945/2 "2016-12-14T15:47:15Z")

</div>

Maybe use `fieldnames` to iterate over all fields, which you get and set with `getfield` and `setfield!`.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 14, 2016, 4:58pm UTC](https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945/3 "2016-12-14T16:58:23Z")

</div>

> [@mauro3](#):
>
> Maybe use fieldnames to iterate over all fields, which you get and set with getfield and setfield!.

Looping over `fieldnames ` with `getfield` and `setfield!` is not type-stable so it’s slower. I agree that it would be nice to have a generic version of this.

It can probably be made using a generated function.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 14, 2016, 6:02pm UTC](https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945/4 "2016-12-14T18:02:53Z")

</div>

If you’re worried about speed, you could create the `copy!` function with a macro at type creation.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [December 14, 2016, 8:54pm UTC](https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945/5 "2016-12-14T20:54:25Z")

</div>

> If you’re worried about speed, you could create the copy! function with a macro at type creation.

I needed something similar, so generated `copy!` function with following.  
You might be able to model something for you use case.

```julia

function CompositeCopy!(T::Symbol)
    dataType = eval(current_module(), T)
    fieldNames = fieldnames(dataType)
    fieldTypes = dataType.types
    expressions = Array(Expr, numFields)

    for i = 1 : length(fieldNames)
        fieldName = fieldNames[i]
        fieldType = fieldTypes[i]
        @assert fieldType.mutable == method_exists(copy!, (fieldType, fieldType))

        if method_exists(copy!, (fieldType, fieldType))
            expressions[i] = :(copy!(x.$fieldName, y.$fieldName))
        else
            expressions[i] = :(x.$fieldName = y.$fieldName)
        end
    end

    body = Expr(:block, expressions...)

    quote
        function Base.copy!(x::$T, y::$T)
            $body
            return x
        end
    end
end

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 14, 2016, 9:18pm UTC](https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945/6 "2016-12-14T21:18:09Z")

</div>

Why don’t you make your example into a `@generated` function? This is what they are for.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [December 14, 2016, 11:24pm UTC](https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945/7 "2016-12-14T23:24:06Z")

</div>

Yes, I probably should make this a generated function.

I think I coded this in v0.3 before generated functions.  
And then I vaguely remember a push limit use of generated functions. Perhaps something to do precompilation?  
Are there any drawbacks to generated functions?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 15, 2016, 1:45am UTC](https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945/8 "2016-12-15T01:45:20Z")

</div>

Hi guys!

Thanks for the answers. I will try to adapt your code @greg_plowman for my needs, Thanks!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 15, 2016, 1:54am UTC](https://discourse.julialang.org/t/how-to-copy-all-fields-without-changing-the-referece/945/9 "2016-12-15T01:54:59Z")

</div>

> [@greg\_plowman](#):
>
> Are there any drawbacks to generated functions?

They require the Julia runtime so you cannot statically compile code which has them I think. Other than that? I don’t think so?
