ConstructionBase.jl icon indicating copy to clipboard operation
ConstructionBase.jl copied to clipboard

API for internally calculated fields

Open tpapp opened this issue 3 years ago • 3 comments

I read the docs but it is still not clear how one should define the relevant interface for types that have internally calculated fields. Eg

struct Foo
    a
    b
    c
    Foo(a, b) = new(a, b, hypot(a, b))
end

I want the user to be able to modify (= create a new immutable with) a and b, and pretend that c does not even exist.

tpapp avatar May 10 '22 09:05 tpapp

I agree this is a pain point. What I generally recommend is using setproperties/getproperties for public access and constructorof for private access. See also https://github.com/JuliaObjects/ConstructionBase.jl/pull/54#issuecomment-1088765017

jw3126 avatar May 10 '22 09:05 jw3126

Sorry if I am missing the obvious, but could you please provide a suggestion to deal with the above struct so that eg Accessors.@optic _.a etc works to modify a?

tpapp avatar May 10 '22 13:05 tpapp

Did you try to overload ConstructionBase.setproperties?

jw3126 avatar May 10 '22 13:05 jw3126