Skip to content

Commit

Permalink
Define constructorof for Tuple and NamedTuple
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Oct 2, 2019
1 parent ee593e5 commit 169e7e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ConstructionBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 169e7e4

Please sign in to comment.