# Reconstruct() in Parameters.jl with parameters interdependency

**URL:** <https://discourse.julialang.org/t/reconstruct-in-parameters-jl-with-parameters-interdependency/47753>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 4, 2020, 11:56pm UTC](https://discourse.julialang.org/t/reconstruct-in-parameters-jl-with-parameters-interdependency/47753 "2020-10-04T23:56:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caio\_Machado](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caio_machado/32/10391_2.png) [@Caio\_Machado](https://discourse.julialang.org/u/Caio_Machado)\
**Post date:** [October 4, 2020, 11:56pm UTC](https://discourse.julialang.org/t/reconstruct-in-parameters-jl-with-parameters-interdependency/47753/1 "2020-10-04T23:56:44Z")

</div>

Hi all,

I find Parameters.jl and its function reconstruct() a very convenient way to handle models that need to be computed under many set of parameters. The feature that one can define a default value for a parameter that is a function of other parameters is really useful. However, consider the following:

```julia
using Parameters

@with_kw struct Para
    a::Float64
    b = a + 1
end
p = Para(a=1)

```

Which returns a struct with a = 1 and b = 2. Now if one uses reconstruct:

```julia
p2 = reconstruct(p,a=5)

```

I would like p2 to return a struct with a = 5 and b = 6, however it returns a struct with a = 5 and b = 2 (b is not updated to the new value of a). In my opinion, for most applications it would make much more sense that reconstruct makes b still consistent with the parameter a. Does anyone know any other option to achieve the desired result (in as simple and parsimonious way)?

---

<div class="post-metadata">

**Author:** ![timquelch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timquelch/32/18278_2.png) [@timquelch](https://discourse.julialang.org/u/timquelch)\
**Post date:** [October 5, 2020, 12:41am UTC](https://discourse.julialang.org/t/reconstruct-in-parameters-jl-with-parameters-interdependency/47753/2 "2020-10-05T00:41:22Z")

</div>

In this case if `b` is always a function of `a` I would just define it as a function

```julia
using Parameters

@with_kw struct Para
    a::Float64
end
b(p) = p.a + 1

p = Para(a=1)

```

```julia
julia> p = Para(a=1);
julia> b(p)
2.0
julia> p2 = reconstruct(p, a=5);
julia> b(p2)
6.0

```

Although I’ll grant that this might not be practical if b is more expensive to compute.

---

<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:** [October 5, 2020, 7:13am UTC](https://discourse.julialang.org/t/reconstruct-in-parameters-jl-with-parameters-interdependency/47753/3 "2020-10-05T07:13:40Z")

</div>

Another way would be to use `getproperty`. (Which arguably should be used for computed fields.)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 5, 2020, 7:32am UTC](https://discourse.julialang.org/t/reconstruct-in-parameters-jl-with-parameters-interdependency/47753/4 "2020-10-05T07:32:58Z")

</div>

For extensive sets of parameters with precomputed quantities, I usually try to organize my code like this:

```julia
struct Parameters{...}
    ...
end

struct PrecomputedModel{TP <: Parameters, ...}
    parameters::TP
    ...
end

function PrecomputedModel(parameters::Parameters)
     ... # do the precalculations
end

```

Then the two sets of quantities are organized separately, which I find leads to cleaner code.

---

<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:** [October 5, 2020, 9:24am UTC](https://discourse.julialang.org/t/reconstruct-in-parameters-jl-with-parameters-interdependency/47753/5 "2020-10-05T09:24:48Z")

</div>

Just from today, maybe of relevance

[https://discourse.julialang.org/t/ann-lazilyinitializedfields-jl-handling-lazily-initialized-fields-in-a-consistent-way](https://discourse.julialang.org/t/ann-lazilyinitializedfields-jl-handling-lazily-initialized-fields-in-a-consistent-way)
