diff --git a/base/namedtuple.jl b/base/namedtuple.jl index 67eaaa9efe941c..2a90466e721632 100644 --- a/base/namedtuple.jl +++ b/base/namedtuple.jl @@ -103,6 +103,8 @@ firstindex(t::NamedTuple) = 1 lastindex(t::NamedTuple) = nfields(t) getindex(t::NamedTuple, i::Int) = getfield(t, i) getindex(t::NamedTuple, i::Symbol) = getfield(t, i) +getindex(t::NamedTuple, v::AbstractVector{<:Integer}) = (; map(i->keys(t)[i] => t[i], v)...) +getindex(t::NamedTuple, v::AbstractVector{Symbol}) = (; map(i->i => t[i], v)...) indexed_iterate(t::NamedTuple, i::Int, state=1) = (getfield(t, i), i+1) isempty(::NamedTuple{()}) = true isempty(::NamedTuple) = false diff --git a/test/namedtuple.jl b/test/namedtuple.jl index 82e711d625cbc8..d9527fdd53ee14 100644 --- a/test/namedtuple.jl +++ b/test/namedtuple.jl @@ -21,6 +21,10 @@ @test (a=3,)[:a] == 3 @test (x=4, y=5, z=6).y == 5 @test (x=4, y=5, z=6).z == 6 +@test (x=4, y=5, z=6)[1:1] == (x=4,) +@test (x=4, y=5, z=6)[1:2] == (x=4, y=5) +@test (x=4, y=5, z=6)[[1,3]] == (x=4, z=6) +@test (x=4, y=5, z=6)[[:x,:y]] == (x=4, y=5) @test_throws ErrorException (x=4, y=5, z=6).a @test_throws BoundsError (a=2,)[0] @test_throws BoundsError (a=2,)[2]