Skip to content

Commit

Permalink
simplify StructArrays handling
Browse files Browse the repository at this point in the history
can just rely on the ConstructionBase support there
  • Loading branch information
aplavin committed Dec 5, 2024
1 parent 30811d8 commit d8d9d03
Showing 1 changed file with 1 addition and 14 deletions.
15 changes: 1 addition & 14 deletions ext/AccessorsStructArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ Accessors.delete(x::StructArray{<:Union{Tuple, NamedTuple}}, o::PropertyLens) =

Accessors.set(x::StructArray{<:Union{Tuple, NamedTuple}}, ::typeof(propertynames), names) = set(x, propertynames StructArrays.components, names)

# (named)tuple eltypes: only component arrays themselves are needed in the constructor
# can change component number/names
Accessors.set(x::StructArray{<:Union{Tuple, NamedTuple}}, ::typeof(StructArrays.components), v) = StructArray(v)
Accessors.set(x::StructArray, ::typeof(StructArrays.components), v) = constructorof(typeof(x))(v)

# other eltypes: need to pass eltype to the constructor in addition to component arrays
# component number/names stay the same
function Accessors.set(x::StructArray{T}, ::typeof(StructArrays.components), v::VT) where {T, VT}
# resulting eltype is basically T, but potentially with different type parameters
# probe its constructorof to get the right concrete type
ET = Base.promote_op(constructorof(T), map(eltype, _eltypes(VT))...)
StructArray{ET}(v)
end

_eltypes(::Type{T}) where {T <: Tuple} = Tuple(T.parameters)
_eltypes(::Type{NamedTuple{K, T}}) where {K, T <: Tuple} = Tuple(T.parameters)
end

0 comments on commit d8d9d03

Please sign in to comment.