Skip to content

Commit 23da960

Browse files
authored
NamedTuple constructor for iterables (#26914)
1 parent d3c99a7 commit 23da960

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

base/namedtuple.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ function NamedTuple{names}(nt::NamedTuple) where {names}
8989
end
9090
end
9191

92+
NamedTuple{names, T}(itr) where {names, T <: Tuple} = NamedTuple{names, T}(T(itr))
93+
NamedTuple{names}(itr) where {names} = NamedTuple{names}(Tuple(itr))
94+
9295
end # if Base
9396

9497
length(t::NamedTuple) = nfields(t)

test/namedtuple.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,7 @@ y = map(v -> (a=v.a, b=v.a + v.b), [(a=1, b=missing), (a=1, b=2)])
236236
# issue #27187
237237
@test reduce(merge,[(a = 1, b = 2), (c = 3, d = 4)]) == (a = 1, b = 2, c = 3, d = 4)
238238
@test typeintersect(NamedTuple{()}, NamedTuple{names, Tuple{Int,Int}} where names) == Union{}
239+
240+
# Iterator constructor
241+
@test NamedTuple{(:a, :b), Tuple{Int, Float64}}(Any[1.0, 2]) === (a=1, b=2.0)
242+
@test NamedTuple{(:a, :b)}(Any[1.0, 2]) === (a=1.0, b=2)

0 commit comments

Comments
 (0)