Automatic resizing of struct internal array when value changes?

Both getproperty and setproperty! are kind of fiddly to get right, since they inherently rely on constant propagation of the accessed field for their type stability. You can write what you want to achieve like this:

function Base.setproperty!(ts::TheStruct, s::Symbol, v)
    if s === :N
        setfield!(ts, s, v) 
        arr = getfield(ts, :x) 
        resize!(arr, v)
    else
        setfield!(ts, s, v) 
    end
end

There’s probably a race condition here in multithreaded code. If I may ask, what are you trying to learn about using this?

1 Like