Skip to content

Commit 1949ebe

Browse files
aplavinnsajkomateuszbaran
authored
define propertynames() to be consistent with getproperty() (#1289)
* define propertynames() to be consistent with getproperty() * Update src/MVector.jl Co-authored-by: Neven Sajko <4944410+nsajko@users.noreply.github.com> * handle :data * Update src/MVector.jl Co-authored-by: Neven Sajko <4944410+nsajko@users.noreply.github.com> * fix * bump version * Update Project.toml --------- Co-authored-by: Neven Sajko <4944410+nsajko@users.noreply.github.com> Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com>
1 parent f4dbc15 commit 1949ebe

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.9.10"
3+
version = "1.9.11"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/MVector.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ end
1818
let dimension_names = QuoteNode.([:x, :y, :z, :w])
1919
body = :(getfield(v, name))
2020
for (i,dim_name) in enumerate(dimension_names)
21+
@eval @inline function Base.propertynames(v::Union{SVector{$i},MVector{$i}}, private::Bool = false)
22+
named_dims = ($(first(dimension_names, i)...),)
23+
private ? (named_dims..., :data) : named_dims
24+
end
25+
2126
body = :(name === $(dimension_names[i]) ? getfield(v, :data)[$i] : $body)
2227
@eval @inline function Base.getproperty(v::Union{SVector{$i},MVector{$i}},
2328
name::Symbol)
@@ -33,3 +38,6 @@ let dimension_names = QuoteNode.([:x, :y, :z, :w])
3338
end
3439
end
3540
end
41+
42+
# for longer S/MVectors and other S/MArrays, the only property is data, and it's private
43+
Base.propertynames(::Union{SArray,MArray}, private::Bool = false) = private ? (:data,) : ()

test/SVector.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,19 @@
151151
end
152152

153153
@testset "Named field access - getproperty" begin
154+
@test propertynames(SA[1,2,3,4,5]) == ()
155+
@test propertynames(SA[1,2,3,4,5], true) == (:data,)
156+
@test propertynames(SA[1 2; 3 4], true) == (:data,)
154157
v4 = SA[10,20,30,40]
158+
@test propertynames(v4) == (:x, :y, :z, :w)
159+
@test propertynames(v4, true) == (:x, :y, :z, :w, :data)
155160
@test v4.x == 10
156161
@test v4.y == 20
157162
@test v4.z == 30
158163
@test v4.w == 40
159164
v2 = SA[10,20]
165+
@test propertynames(v2) == (:x, :y)
166+
@test propertynames(v2, true) == (:x, :y, :data)
160167
@test v2.x == 10
161168
@test v2.y == 20
162169
@test_throws ErrorException v2.z

0 commit comments

Comments
 (0)