From 169e7e47d1f566ae16624089bff359a1a02eb092 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Tue, 1 Oct 2019 21:45:55 -0700 Subject: [PATCH] Define constructorof for Tuple and NamedTuple --- src/ConstructionBase.jl | 4 ++++ test/runtests.jl | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/ConstructionBase.jl b/src/ConstructionBase.jl index 293858e..ac1e2a6 100644 --- a/src/ConstructionBase.jl +++ b/src/ConstructionBase.jl @@ -72,6 +72,10 @@ T{Int64,Int64}(10, 2) getfield(parentmodule(T), nameof(T)) end +constructorof(::Type{<:Tuple}) = tuple +constructorof(::Type{<:NamedTuple{names}}) where names = + NamedTuple{names} ∘ tuple + function assert_hasfields(T, fnames) for fname in fnames if !(fname in fieldnames(T)) diff --git a/test/runtests.jl b/test/runtests.jl index b46b4da..42e3dfb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,6 +12,10 @@ end @inferred constructorof(AB{Int, Int}) @test constructorof(AB{Int, Int})(1, 2) === AB(1,2) @test constructorof(AB{Int, Int})(1.0, 2) === AB(1.0,2) + @test constructorof(typeof((a=1, b=2)))(1.0, 2) === (a=1.0, b=2) + @test constructorof(NamedTuple{(:a, :b)})(1.0, 2) === (a=1.0, b=2) + @test constructorof(Tuple)(1.0, 2) === (1.0, 2) + @test constructorof(Tuple{Nothing, Missing})(1.0, 2) === (1.0, 2) end @testset "setproperties" begin