# Automatic update of the values of a structure

**URL:** <https://discourse.julialang.org/t/automatic-update-of-the-values-of-a-structure/95500>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [March 3, 2023, 2:51pm UTC](https://discourse.julialang.org/t/automatic-update-of-the-values-of-a-structure/95500 "2023-03-03T14:51:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)\
**Post date:** [March 3, 2023, 2:51pm UTC](https://discourse.julialang.org/t/automatic-update-of-the-values-of-a-structure/95500/1 "2023-03-03T14:51:28Z")

</div>

Hello 😁,  
I would like to know if it is possible to automatically update the parameters of a strcutre when it is modified. For example, in the following example, the value of b is not updated when a changes:

```julia
julia> Base.@kwdef mutable struct S
                  a
                  b = 3 * a
                  end
S

julia> s = S(a=5)
S(5, 15)

julia> s.a = 10
10

julia> s
S(10, 15)

```

Would it be possible to make b automatically value` 3 * 10 = 30` ?

Thanks ! 😁  
fdekerm

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 3, 2023, 2:55pm UTC](https://discourse.julialang.org/t/automatic-update-of-the-values-of-a-structure/95500/2 "2023-03-03T14:55:39Z")

</div>

Generally the better way to do this is to make `b` be a property overload. i.e.

```julia
mutable struct S
    a
end

Base.propertynames(::S) = (:a, :b)
function Base.getproperty(s::S, p::Symbol)
    if p == :b
        return 3 * getfield(s, :a)
    end
    return getfield(s, p)
end

```

---

<div class="post-metadata">

**Author:** ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)\
**Post date:** [March 3, 2023, 3:13pm UTC](https://discourse.julialang.org/t/automatic-update-of-the-values-of-a-structure/95500/3 "2023-03-03T15:13:06Z")

</div>

> [@Oscar\_Smith](#):
>
> property overload

Wow great thanks a lot!

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [March 3, 2023, 3:16pm UTC](https://discourse.julialang.org/t/automatic-update-of-the-values-of-a-structure/95500/4 "2023-03-03T15:16:27Z")

</div>

The above-mentioned method of `Base.getproperty` overloading is best.

The alternative would be to make a specialized method for `Base.setproperty!(s::S, field::Symbol, value)`, but that will be more complicated and easier to break.
