diff --git a/src/gentypes.jl b/src/gentypes.jl index 02bd0df..1c32a06 100644 --- a/src/gentypes.jl +++ b/src/gentypes.jl @@ -12,8 +12,13 @@ struct Top end get_type(NT, k) = hasfield(NT, k) ? fieldtype(NT, k) : Nothing # unify two types to a single type +function promoteunion(T, S) + new = promote_type(T, S) + return isabstracttype(new) ? Union{T, S} : new +end + unify(a, b) = unify(b, a) -unify(a::Type{T}, b::Type{S}) where {T,S} = Base.promote_typejoin(T, S) +unify(a::Type{T}, b::Type{S}) where {T,S} = promoteunion(T, S) unify(a::Type{T}, b::Type{S}) where {T,S<:T} = T unify(a::Type{Top}, b::Type{T}) where {T} = T diff --git a/test/gentypes.jl b/test/gentypes.jl index e0e4a13..9cf6897 100644 --- a/test/gentypes.jl +++ b/test/gentypes.jl @@ -252,6 +252,7 @@ @test !(JSONTypes.Root.mutable) @test parsed[1].c.d == 4 + @test fieldtype(JSONTypes.Root, 1) == Union{Int64, String} end @testset "Raw Types" begin