Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/StructTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ Note that any `StructTypes.names` mappings are applied, as well as field-specifi
"""
@inline function construct(f, ::Type{T}) where {T}
N = fieldcount(T)
N == 0 && return T()
N == 0 && return T <: NamedTuple ? T(()) : T()
nms = names(T)
kwargs = keywordargs(T)
constructor = T <: Tuple ? tuple : T <: NamedTuple ? ((x...) -> T(tuple(x...))) : T
Expand Down Expand Up @@ -895,7 +895,7 @@ end

@inline function construct(values::Vector{Any}, ::Type{T}) where {T}
N = fieldcount(T)
N == 0 && return T()
N == 0 && return T <: NamedTuple ? T(()) : T()
nms = names(T)
kwargs = keywordargs(T)
constructor = T <: Tuple ? tuple : T <: NamedTuple ? ((x...) -> T(tuple(x...))) : T
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ v = v"1.2.3"

# 47
@test StructTypes.construct(Nothing, nothing) === nothing

# types with no fields, including the empty NamedTuple
@test StructTypes.construct((i, nm, FT) -> error("no fields"), NamedTuple{(), Tuple{}}) === NamedTuple()
@test StructTypes.construct(Any[], NamedTuple{(), Tuple{}}) === NamedTuple()
@test StructTypes.construct(Any[], Tuple{}) === ()
@test StructTypes.constructfrom(NamedTuple{(), Tuple{}}, Dict{String, Any}()) === NamedTuple()
x = Dict("hey" => "ho")
@test StructTypes.construct(Dict{String, String}, x) === x

Expand Down
Loading