Skip to content

Commit 171d06a

Browse files
committed
support widening staticarray eltypes
1 parent cc9f984 commit 171d06a

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

ext/AccessorsStaticArraysExt.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ isdefined(Base, :get_extension) ? (using StaticArrays) : (using ..StaticArrays)
33
using Accessors
44
import Accessors: setindex, delete, insert
55

6-
@inline setindex(a::StaticArray, args...) = Base.setindex(a, args...)
6+
@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...)
7+
@inline setindex(a::StaticArray{<:Any,T}, v::T, I...) where {T} = Base.setindex(a, v, I...)
78
@inline delete(obj::StaticVector, l::IndexLens) = StaticArrays.deleteat(obj, only(l.indices))
8-
@inline insert(obj::StaticVector, l::IndexLens, val) = StaticArrays.insert(obj, only(l.indices), val)
9+
@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)
10+
@inline insert(obj::StaticVector{<:Any,T}, l::IndexLens, val::T) where {T} = StaticArrays.insert(obj, only(l.indices), val)
911

1012
Accessors.set(obj::StaticVector, ::Type{Tuple}, val::Tuple) = constructorof(typeof(obj))(val...)
1113
Accessors.set(obj::Tuple, ::Type{<:StaticVector}, val::StaticVector) = Tuple(val)

test/test_extensions.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ end
8383
@test setindex(obj, 5, 2, 1) === StaticArrays.@SMatrix [1 2; 5 4]
8484
end
8585

86+
test_getset_laws((@optic _[1]), (@SVector [1,2,3]), 1.5, 2.5)
87+
test_insertdelete_laws((@optic _[1]), (@SVector [1,2,3]), 1.5)
88+
8689
v = @SVector [1.,2,3]
8790
@test (@set v[1] = 10) === @SVector [10.,2,3]
8891
@test (@set v[1] = π) === @SVector [π,2,3]

0 commit comments

Comments
 (0)