# Struct copy with change in the value of fields

**URL:** https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592
**Category:** Performance
**Created:** [January 13, 2019, 8:58pm UTC](https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592 "2019-01-13T20:58:43Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [January 13, 2019, 8:58pm UTC](https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592/1 "2019-01-13T20:58:43Z")

</div>

Hi,

I have an anoying situation where I want to copy a variable of composed type by changing a value of the field which may in turn change the type. Here is an example

```julia

# in my application, the following struct contains ~20 fields...
struct A{T,vectype}
    p::T
    v::vectype
end

a = A(Int32(1), rand(10))

```

is of type `A{Int32,Array{Float64,1}}`. What I would like to do is something like this

```julia
b = copy(a, p = 0.21)

```

I face two issues. The first is I want to change an element, so writing this is anoying

```julia
b = A(p = 0.21, v = a.v) # I would have to write every field :frowning:

```

I cant do

```julia
b = deepcopy(a)
b.p = 0.21

```

as the types dont match.

Does anybody have a solution please?

Thank you,

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [January 13, 2019, 9:10pm UTC](https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592/2 "2019-01-13T21:10:04Z")

</div>

Use Setfield.jl:

```julia
julia> using Setfield

julia> b = @set a.p = 0.21;

julia> typeof(b)
A{Float64,Array{Float64,1}}

```

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [January 13, 2019, 9:38pm UTC](https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592/3 "2019-01-13T21:38:08Z")

</div>

Thank you!

---

<div class="post-metadata">

### Author: ![raythurnevoid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raythurnevoid/32/52651_2.png) [@raythurnevoid](https://discourse.julialang.org/u/raythurnevoid)
#### Post date: [September 27, 2023, 1:14am UTC](https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592/4 "2023-09-27T01:14:36Z")

</div>

[JuliaObjects/Accessors.jl: Update immutable data (github.com)](https://github.com/JuliaObjects/Accessors.jl) as per 2023

---

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [September 27, 2023, 1:15pm UTC](https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592/5 "2023-09-27T13:15:09Z")

</div>

You can define a constructor function to create a new struct instance using an old one’s default values. If you have a struct `S`:

```julia
struct S
    a
    b
    S(a, b) = new(a, b)
end

```

You can define:

```julia
S(x::S; a=x.a, b=x.b) = S(a, b)

```

Then:

```julia
old = S("a", "b") # S("a", "b")
new = S(old; b=2) # S("a", 2)

```

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [September 29, 2023, 1:39pm UTC](https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592/6 "2023-09-29T13:39:08Z")

</div>

yeah but imagine that there are 50 fields

---

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [September 29, 2023, 11:04pm UTC](https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592/7 "2023-09-29T23:04:58Z")

</div>

It’s a pain if you have 50 fields. But if you just have one struct for which you need this, and don’t wish to download a package just for this one struct, this is an option.

This is an old post and looks like you found a solution. I just wanted to write my response in case someone with a similar problem lands on this thread. They can make a more informed choice knowing there are simpler options for simpler cases.

---

<div class="post-metadata">

### Author: ![Nikos\_Gianniotis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nikos_gianniotis/32/11487_2.png) [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)
#### Post date: [October 15, 2024, 8:32am UTC](https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592/8 "2024-10-15T08:32:33Z")

</div>

Thanks, this works well for me.

---

<div class="post-metadata">

### Author: ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)
#### Post date: [August 14, 2025, 12:33am UTC](https://discourse.julialang.org/t/struct-copy-with-change-in-the-value-of-fields/19592/9 "2025-08-14T00:33:29Z")

</div>

In case you want something pretty general but don’t want to bring in a package, here’s a simple function that does this for you, assuming you can use keyword arguments for your constructor:

```julia-auto
julia> @kwdef struct AType; a::Float64; b::Int64; end;

julia> x = AType(1., 2);

julia> function copy_with_exceptions(x::T; kwargs...) where {T}
           return T(; (f => getfield(x, f) for f in fieldnames(T))..., kwargs...)
       end;

julia> copy_with_exceptions(x; b = 3)
AType(1.0, 3)

```

I’m not sure about performance here, but the pattern is simple enough if you don’t want to bring in Accessors.jl.
