diff --git a/src/StructTypes.jl b/src/StructTypes.jl index e1b7325..a83ed71 100644 --- a/src/StructTypes.jl +++ b/src/StructTypes.jl @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index adf3c57..145d59d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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