Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify StructArrays handling #182

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading