Skip to content

Commit

Permalink
Add Base.tail(::NamedTuple). (#29595)
Browse files Browse the repository at this point in the history
* Add Base.tail(::NamedTuple).

Fixes #29572.

* Simplify, as suggested by @JeffBezanson.
  • Loading branch information
tpapp authored and KristofferC committed Oct 26, 2018
1 parent ffaf849 commit 010495f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ values(nt::NamedTuple) = Tuple(nt)
haskey(nt::NamedTuple, key::Union{Integer, Symbol}) = isdefined(nt, key)
get(nt::NamedTuple, key::Union{Integer, Symbol}, default) = haskey(nt, key) ? getfield(nt, key) : default
get(f::Callable, nt::NamedTuple, key::Union{Integer, Symbol}) = haskey(nt, key) ? getfield(nt, key) : f()
tail(t::NamedTuple{names}) where names = NamedTuple{tail(names)}(t)

@pure function diff_names(an::Tuple{Vararg{Symbol}}, bn::Tuple{Vararg{Symbol}})
names = Symbol[]
Expand Down
3 changes: 3 additions & 0 deletions test/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ end
@test get(()->0, (a=1, b=2, c=3), :a) == 1
@test get(()->0, NamedTuple(), :a) == 0
@test get(()->0, (a=1,), :b) == 0
@test Base.tail((a = 1, b = 2.0, c = 'x')) (b = 2.0, c = 'x')
@test Base.tail((a = 1, )) NamedTuple()
@test_throws MethodError Base.tail(NamedTuple())

# syntax errors

Expand Down

0 comments on commit 010495f

Please sign in to comment.