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

define propertynames() to be consistent with getproperty() #1289

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions src/MVector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
let dimension_names = QuoteNode.([:x, :y, :z, :w])
body = :(getfield(v, name))
for (i,dim_name) in enumerate(dimension_names)
@eval @inline Base.propertynames(v::Union{SVector{$i},MVector{$i}}, private::Bool = false) =

Check warning on line 21 in src/MVector.jl

View check run for this annotation

Codecov / codecov/patch

src/MVector.jl#L21

Added line #L21 was not covered by tests
private ? ($(first(dimension_names, i)...), :data) : ($(first(dimension_names, i)...),)
aplavin marked this conversation as resolved.
Show resolved Hide resolved

body = :(name === $(dimension_names[i]) ? getfield(v, :data)[$i] : $body)
@eval @inline function Base.getproperty(v::Union{SVector{$i},MVector{$i}},
name::Symbol)
Expand All @@ -33,3 +36,6 @@
end
end
end

# for longer S/MVectors and other S/MArrays, the only property is data, and it's private
Base.propertynames(::Union{SArray,MArray}, private::Bool = false) = private ? (:data,) : ()

Check warning on line 41 in src/MVector.jl

View check run for this annotation

Codecov / codecov/patch

src/MVector.jl#L41

Added line #L41 was not covered by tests
7 changes: 7 additions & 0 deletions test/SVector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,19 @@
end

@testset "Named field access - getproperty" begin
@test propertynames(SA[1,2,3,4,5]) == ()
@test propertynames(SA[1,2,3,4,5], true) == (:data,)
@test propertynames(SA[1 2; 3 4], true) == (:data,)
v4 = SA[10,20,30,40]
@test propertynames(v4) == (:x, :y, :z, :w)
@test propertynames(v4, true) == (:x, :y, :z, :w, :data)
@test v4.x == 10
@test v4.y == 20
@test v4.z == 30
@test v4.w == 40
v2 = SA[10,20]
@test propertynames(v2) == (:x, :y)
@test propertynames(v2, true) == (:x, :y, :data)
@test v2.x == 10
@test v2.y == 20
@test_throws ErrorException v2.z
Expand Down
Loading