Skip to content

Commit 3e3a68b

Browse files
committed
named properties for staticarrays
1 parent 033ff58 commit 3e3a68b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

ext/StructArraysStaticArraysCoreExt.jl

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using StructArrays: StructArrayStyle, createinstance, replace_structarray, isnon
55

66
using Base.Broadcast: Broadcasted
77

8-
using StaticArraysCore: StaticArray, FieldArray, tuple_prod
8+
using StaticArraysCore: StaticArray, FieldArray, tuple_prod, SVector, MVector
99

1010
"""
1111
StructArrays.staticschema(::Type{<:StaticArray{S, T}}) where {S, T}
@@ -74,4 +74,13 @@ end
7474
end
7575
end
7676

77+
@inline function StructArrays.component(s::StructArray{<:Union{SVector,MVector}}, key::Symbol)
78+
i = key == :x ? 1 :
79+
key == :y ? 2 :
80+
key == :z ? 3 :
81+
key == :w ? 4 :
82+
throw(ArgumentError("invalid key $key"))
83+
StructArrays.component(s, i)
84+
end
85+
7786
end # module

test/runtests.jl

+6
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,12 @@ end
13811381
@test StructArrays.components(x) == ([1.0,2.0], [2.0,3.0])
13821382
@test x .+ y == StructArray([StaticVectorType{2}(Float64[2*i+1;2*i+3]) for i = 1:2])
13831383
end
1384+
for StaticVectorType = [SVector, MVector]
1385+
x = @inferred StructArray([StaticVectorType{2}(Float64[i;i+1]) for i = 1:2])
1386+
# numbered and named property access:
1387+
@test x.:1 == [1.0,2.0]
1388+
@test x.y == [2.0,3.0]
1389+
end
13841390
# test broadcast + components for general arrays
13851391
for StaticArrayType = [SArray, MArray, SizedArray]
13861392
x = @inferred StructArray([StaticArrayType{Tuple{1,2}}(ones(1,2) .+ i) for i = 0:1])

0 commit comments

Comments
 (0)