Skip to content

Commit 073cbbb

Browse files
tkfJeffBezanson
authored andcommitted
Add empty(::NamedTuple) (#32534)
1 parent 13db0dc commit 073cbbb

File tree

3 files changed

+4
-0
lines changed

3 files changed

+4
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Standard library changes
4040
when operating over zero-dimensional arrays ([#32122]).
4141
* `IPAddr` subtypes now behave like scalars when used in broadcasting ([#32133]).
4242
* `clamp` can now handle missing values ([#31066]).
43+
* `empty` now accepts a `NamedTuple` ([#32534])
4344

4445
#### Libdl
4546

base/namedtuple.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ getindex(t::NamedTuple, i::Symbol) = getfield(t, i)
106106
indexed_iterate(t::NamedTuple, i::Int, state=1) = (getfield(t, i), i+1)
107107
isempty(::NamedTuple{()}) = true
108108
isempty(::NamedTuple) = false
109+
empty(::NamedTuple) = NamedTuple()
109110

110111
convert(::Type{NamedTuple{names,T}}, nt::NamedTuple{names,T}) where {names,T<:Tuple} = nt
111112
convert(::Type{NamedTuple{names}}, nt::NamedTuple{names}) where {names} = nt

test/namedtuple.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
@test isempty(NamedTuple())
4040
@test !isempty((a=1,))
41+
@test empty((a=1,)) === NamedTuple()
42+
@test isempty(empty((a=1,)))
4143

4244
@test (a=1,b=2) === (a=1,b=2)
4345
@test (a=1,b=2) !== (b=1,a=2)

0 commit comments

Comments
 (0)