quinnj / JSON3.jl

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generate Union types instead of Any

kskyten opened this issue · comments

This test https://github.com/quinnj/JSON3.jl/blob/main/test/gentypes.jl#L236 for code generation generates this root struct:

struct Root
    a::Any
    b::Int64
    c::C
end

Would it make more sense to generate small union types instead of Any? i.e

struct Root
    a::Union{String, Int}
    b::Int64
    c::C
end

@quinnj I believe this one stems from the type of the parsed JSON field as opposed to the unioning in the type-generation, do you know if we can easily be more specific on the JSON3.read?

This is due to the use of Base.promote_typejoin in the unify method. I think doing something like #144 would be preferrable. Basically we try to promote and if that gives us a concrete type, then use that (for example, unify(Int, Float64) => Float64). Otherwise, let's prefer a Union.