Manipulating fields of a StructArray does not work

I create a StructArray and want to manipulate the field value of one element.

julia> using StructArrays
julia> mutable struct Test
           a
           b
         end

julia> arr = [Test(1,1), Test(2,2)]
2-element Array{Test,1}:
 Test(1, 1)
 Test(2, 2)

julia>

julia> sarr = StructArray(arr)
2-element StructArray{Test,1,NamedTuple{(:a, :b),Tuple{Array{Any,1},Array{Any,1}}}}:
 Test(1, 1)
 Test(2, 2)

julia>

julia> sarr[1].a = 5
5

julia>

julia> sarr.a
2-element Array{Any,1}:
 1
 2

Why does this not work?

sarr[1].a creates a new instance of Test by collecting the fields in the different arrays. You want to do:

julia> sarr.a[1] = 5
5

julia> sarr.a
2-element Array{Any,1}:
 5
 2

Also, if you are interested in performance, StructArray works much better with immutable (isbitstype) structs.

1 Like