Struct copy with change in the value of fields



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

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

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

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

b = copy(a, p = 0.21)

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

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

I cant do

b = deepcopy(a)
b.p = 0.21

as the types dont match.

Does anybody have a solution please?

Thank you,


Use Setfield.jl:

julia> using Setfield

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

julia> typeof(b)


Thank you!