I am starting to use StructArrays
in a package, and the benefits clearly outweigh the costs. However, there is one thing that I have, for the moment, to let go, which is the mutation of fields using the most natural syntax.
I mean, for example, with an array of mutable structs, I can do:
julia> mutable struct Atom
index
name
end
julia> v1 = [ Atom(1,"C"), Atom(2,"N") ]
2-element Vector{Atom}:
Atom(1, "C")
Atom(2, "N")
julia> v1[1].name = "N" # <--- mutate field name of first element
"N"
julia> v1
2-element Vector{Atom}:
Atom(1, "N")
Atom(2, "N")
With a StructArray
, however, that does not work anymore:
julia> v2 = StructArray(v1)
2-element StructArray(::Vector{Any}, ::Vector{Any}) with eltype Atom:
Atom(1, "N")
Atom(2, "N")
julia> v2[1].name = "C" # <-- same syntax, no error
"C"
julia> v2
2-element StructArray(::Vector{Any}, ::Vector{Any}) with eltype Atom:
Atom(1, "N") # but no mutation :-(
Atom(2, "N")
I know I can do
julia> v2.name[1] = "C"
"C"
julia> v2
2-element StructArray(::Vector{Any}, ::Vector{Any}) with eltype Atom:
Atom(1, "C")
Atom(2, "N")
But that would be a breaking change. Is it possible, easy, and reasonable to recover the original mutation syntax while still using StructArrays
? This was the only thing that broke from what I have been doing with these arrays of structs, everything else worked perfectly, and better.