Skip to content

Commit 9058612

Browse files
committed
add setindex! specialization for VectorOfArray{StructArray}
1 parent 451c79d commit 9058612

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

ext/RecursiveArrayToolsStructArraysExt.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,25 @@ module RecursiveArrayToolsStructArraysExt
33
import RecursiveArrayTools, StructArrays
44
RecursiveArrayTools.rewrap(::StructArrays.StructArray, u) = StructArrays.StructArray(u)
55

6-
end
6+
using RecursiveArrayTools: VectorOfArray
7+
using StructArrays: StructArray
8+
9+
const VectorOfStructArray{T, N} = VectorOfArray{T, N, <:StructArray}
10+
11+
# Since `StructArray` lazily materializes struct entries, the general `setindex!(x, val, I)`
12+
# operation `VA.u[I[end]][Base.front(I)...]` will only update a lazily materialized struct
13+
# entry of `u`, but will not actually mutate `x::StructArray`. See the StructArray documentation
14+
# for more details:
15+
#
16+
# https://juliaarrays.github.io/StructArrays.jl/stable/counterintuitive/#Modifying-a-field-of-a-struct-element
17+
#
18+
# To avoid this, we can materialize a struct entry, modify it, and then use `setindex!`
19+
# with the modified struct entry.
20+
function Base.setindex!(VA::VectorOfStructArray{T, N}, v,
21+
I::Int...) where {T, N}
22+
u_I = VA.u[I[end]]
23+
u_I[Base.front(I)...] = v
24+
return VA.u[I[end]] = u_I
25+
end
26+
27+
end

0 commit comments

Comments
 (0)