mauro3 / Parameters.jl

Types with default field values, keyword constructors and (un-)pack macros
Other
419 stars 31 forks source link

how to make parameter independence work for using copy or reconstruct? #136

Closed vancleve closed 3 years ago

vancleve commented 3 years ago

If there is parameter interdependence like below, updating the value of an independent parameter when copying or reconstructing a parameter object does not update the value of the dependent parameter.

julia> @with_kw struct pars
           a::Float64 = 1.0
           b::Float64 = a + 1.0
       end
pars

julia> x = pars()
pars
  a: Float64 1.0
  b: Float64 2.0

julia> y = pars(x; a=2.0)
pars
  a: Float64 2.0
  b: Float64 2.0

Any ideas how to get around this?

mauro3 commented 3 years ago

No, not really. But you could add an assertion to at least catch it, at least if property b==a+1 must hold.

vancleve commented 3 years ago

Yes, right. One can implement tests on a case-by-case basis. I'll give it a go.

mauro3 commented 3 years ago

Another thing you can do, if it always holds, is to use getproperty and calculated it on the fly.

vancleve commented 3 years ago

ah yes, great idea!