Skip to content

Commit 169e7e4

Browse files
committed
Define constructorof for Tuple and NamedTuple
1 parent ee593e5 commit 169e7e4

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/ConstructionBase.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ T{Int64,Int64}(10, 2)
7272
getfield(parentmodule(T), nameof(T))
7373
end
7474

75+
constructorof(::Type{<:Tuple}) = tuple
76+
constructorof(::Type{<:NamedTuple{names}}) where names =
77+
NamedTuple{names} tuple
78+
7579
function assert_hasfields(T, fnames)
7680
for fname in fnames
7781
if !(fname in fieldnames(T))

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ end
1212
@inferred constructorof(AB{Int, Int})
1313
@test constructorof(AB{Int, Int})(1, 2) === AB(1,2)
1414
@test constructorof(AB{Int, Int})(1.0, 2) === AB(1.0,2)
15+
@test constructorof(typeof((a=1, b=2)))(1.0, 2) === (a=1.0, b=2)
16+
@test constructorof(NamedTuple{(:a, :b)})(1.0, 2) === (a=1.0, b=2)
17+
@test constructorof(Tuple)(1.0, 2) === (1.0, 2)
18+
@test constructorof(Tuple{Nothing, Missing})(1.0, 2) === (1.0, 2)
1519
end
1620

1721
@testset "setproperties" begin

0 commit comments

Comments
 (0)