Skip to content

Commit

Permalink
support view of a single element by names/keys
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed May 27, 2022
1 parent 94dcb6d commit d8d864c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/names.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ end

@inline @propagate_inbounds (A::NdaKa)(args...) = getkey(A, args...)

@inline @propagate_inbounds (A::KaNda)(;kw...) = getkey(A; kw...)
@inline @propagate_inbounds (A::NdaKa)(;kw...) = getkey(A; kw...)
@inline @propagate_inbounds (A::KaNda)(c=nothing; kw...) = getkey(A, c; kw...)
@inline @propagate_inbounds (A::NdaKa)(c=nothing; kw...) = getkey(A, c; kw...)

@inline @propagate_inbounds function getkey(A; kw...)
@inline @propagate_inbounds function getkey(A, c::Union{Nothing, Colon}; kw...)
list = dimnames(A)
issubset(keys(kw), list) || error("some keywords not in list of names!")
args = map(s -> Base.sym_in(s, keys(kw)) ? getfield(values(kw), s) : Colon(), list)
A(args...)
isnothing(c) ? A(args...) : A(args..., c)
end

# Constructors, including pirate method (A; kw...)
Expand Down
4 changes: 4 additions & 0 deletions test/_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ end

@test_throws Exception N(obs=55) # ideally ArgumentError
@test_throws Exception N(obs='z') # ideally BoundsError

Nc = copy(N)
Nc(obs='a', iter=20, :) .= 1000
@test Nc(obs='a', iter=20) == 1000
end

@testset "named_axiskeys" begin
Expand Down

0 comments on commit d8d864c

Please sign in to comment.