Skip to content

Commit

Permalink
support widening staticarray eltypes
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Nov 27, 2024
1 parent 30811d8 commit 43a2574
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ext/AccessorsStaticArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ isdefined(Base, :get_extension) ? (using StaticArrays) : (using ..StaticArrays)
using Accessors
import Accessors: setindex, delete, insert

@inline setindex(a::StaticArray, args...) = Base.setindex(a, args...)
@inline setindex(a::StaticArray{<:Any,ET}, v::T, I...) where {ET,T} = Base.setindex(similar_type(typeof(a), promote_type(ET, T))(a), v, I...)
@inline setindex(a::StaticArray{<:Any,T}, v::T, I...) where {T} = Base.setindex(a, v, I...)
@inline delete(obj::StaticVector, l::IndexLens) = StaticArrays.deleteat(obj, only(l.indices))
@inline insert(obj::StaticVector, l::IndexLens, val) = StaticArrays.insert(obj, only(l.indices), val)
@inline insert(obj::StaticVector{<:Any,ET}, l::IndexLens, val::T) where {ET,T} = StaticArrays.insert(similar_type(typeof(obj), promote_type(ET, T))(obj), only(l.indices), val)
@inline insert(obj::StaticVector{<:Any,T}, l::IndexLens, val::T) where {T} = StaticArrays.insert(obj, only(l.indices), val)

Accessors.set(obj::StaticVector, ::Type{Tuple}, val::Tuple) = constructorof(typeof(obj))(val...)
Accessors.set(obj::Tuple, ::Type{<:StaticVector}, val::StaticVector) = Tuple(val)
Expand Down
3 changes: 3 additions & 0 deletions test/test_extensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ end
@test setindex(obj, 5, 2, 1) === StaticArrays.@SMatrix [1 2; 5 4]
end

test_getset_laws((@optic _[1]), (@SVector [1,2,3]), 1.5, 2.5)
test_insertdelete_laws((@optic _[1]), (@SVector [1,2,3]), 1.5)

v = @SVector [1.,2,3]
@test (@set v[1] = 10) === @SVector [10.,2,3]
@test (@set v[1] = π) === @SVector [π,2,3]
Expand Down

0 comments on commit 43a2574

Please sign in to comment.