# Convenient way to create a copy of an immutable struct with a changed field

**URL:** https://discourse.julialang.org/t/convenient-way-to-create-a-copy-of-an-immutable-struct-with-a-changed-field/28899
**Category:** General Usage
**Created:** [September 18, 2019, 4:02pm UTC](https://discourse.julialang.org/t/convenient-way-to-create-a-copy-of-an-immutable-struct-with-a-changed-field/28899 "2019-09-18T16:02:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [September 18, 2019, 4:02pm UTC](https://discourse.julialang.org/t/convenient-way-to-create-a-copy-of-an-immutable-struct-with-a-changed-field/28899/1 "2019-09-18T16:02:51Z")

</div>

It might sound a bit weird but I am facing the following issue quite often:

Given a largish struct:

```julia
struct Foo
    a
    b
    c
    d
    e
    f
    g
    h
    i
    j
end

```

And a bunch of them in a `Vector`

```julia
foos = [Foo(rand(10)...) for _ ∈ 1:10]

```

I’d like to “alter” e.g. one field (e.g. subtracting 23 from each `.h`), by creating an exact copy with the new values of `.h`.

Something like

```julia
altered_foos = copy(foos, but = :h=>x->x-23)

```

Is there a somewhat convenient method/package or whatever to do this other than writing out the a new constructor? I’d like to avoid code repetition and also tried it via meta programming but I have the feeling this has been solved before.

Edit: no, I don’t want to make it mutable 😉

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 18, 2019, 4:03pm UTC](https://discourse.julialang.org/t/convenient-way-to-create-a-copy-of-an-immutable-struct-with-a-changed-field/28899/2 "2019-09-18T16:03:35Z")

</div>

[https://github.com/jw3126/Setfield.jl](https://github.com/jw3126/Setfield.jl) maybe.

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [September 18, 2019, 4:03pm UTC](https://discourse.julialang.org/t/convenient-way-to-create-a-copy-of-an-immutable-struct-with-a-changed-field/28899/3 "2019-09-18T16:03:56Z")

</div>

That was fast 😂

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [September 18, 2019, 4:04pm UTC](https://discourse.julialang.org/t/convenient-way-to-create-a-copy-of-an-immutable-struct-with-a-changed-field/28899/4 "2019-09-18T16:04:40Z")

</div>

Awesome, it’s like lenses in haskell. Thanks!
