Use with mutable arrays?
If one has a mutable array of immutable objects, is it possible to use Accessors.jl to update just one object in the array without copying the whole array?
julia> struct MyT
x::Int
end
julia> v = [MyT(1), MyT(2)]
2-element Vector{MyT}:
MyT(1)
MyT(2)
julia> @set v[1].x = 5
2-element Vector{MyT}:
MyT(5)
MyT(2)
julia> v
2-element Vector{MyT}:
MyT(1)
MyT(2)
Looks like @set is returning a whole copy of v, not just updating v[1]. Is that right? Is there a way around this?
Personally I usually do
v1 = v[1]
v[1] = @set v1.x = 5
Alternatives would be to use Persistent Arrays for efficient immutable updates of arrays. It should be easy to support FunctionalCollections with Accessors but it is not yet implemented I think. Or defining a custom lens that is allowed to do mutation. If you want to work on one of these, I am happy to help.
Thank you for the response and tip. Right now I'm working on some other personal projects so unfortunately it's unlikely I'll open a PR.
Sure no problem!
Try the view-lens now available in AccessorsExtra:
julia> @set v |> view(_, 1)[].x = 5
2-element Vector{MyT}:
MyT(5)
MyT(2)
julia> v
2-element Vector{MyT}:
MyT(5)
MyT(2)
Suggestions for a cleaner approach that doesn't introduce new structs (for the user to learn) are welcome!