Skip to content

Commit

Permalink
implement convert from NamedTuple to Tuple (#49879)
Browse files Browse the repository at this point in the history
  • Loading branch information
baumgold authored Feb 9, 2024
1 parent daac6a6 commit 65d4ce2
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 @@ -182,6 +182,7 @@ nextind(@nospecialize(t::NamedTuple), i::Integer) = Int(i)+1

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

function convert(::Type{NamedTuple{names,T}}, nt::NamedTuple{names}) where {names,T<:Tuple}
NamedTuple{names,T}(T(nt))::NamedTuple{names,T}
Expand Down
3 changes: 3 additions & 0 deletions test/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ end

conv_res = @test_throws MethodError convert(NamedTuple{(:a,),Tuple{I}} where I<:AbstractString, (;a=1))
@test conv_res.value.f === convert && conv_res.value.args === (AbstractString, 1)

conv6 = convert(NamedTuple{(:a,),Tuple{NamedTuple{(:b,), Tuple{Int}}}}, ((1,),))
@test conv6 === (a = (b = 1,),)
end

@test NamedTuple{(:a,:c)}((b=1,z=2,c=3,aa=4,a=5)) === (a=5, c=3)
Expand Down

0 comments on commit 65d4ce2

Please sign in to comment.