Dict Types Behavior

I was working with Dict recently and found some strange behavior, i dont know if its meant to be this way, but i got curious about the reasoning(if intended).

julia> Dict{Symbol,Union{Missing,Any}}()
Dict{Symbol,Any} with 0 entries

julia> dict = Dict{Symbol,Union{Missing,Any}}()
Dict{Symbol,Any} with 0 entries

julia> push!(dict,:a => missing, :b => "string")
Dict{Symbol,Any} with 2 entries:
  :a => missing
  :b => "string"

Here i pass explicitly the type of the dict, and Julia returns Dict{Symbol, Any}. As missing is a type too, converting to Any would be ok. But here is where i think its inconsistent:

julia> Dict(:a => missing, :b => "string")
Dict{Symbol,Union{Missing, String}} with 2 entries:
  :a => missing
  :b => "string"

In this case, passing directly on the constructor, it’s built with the desired type.

julia> Union{Missing,Any}
Any
3 Likes

Yes, it makes sense, but in the second example shouldnt the Dict have the type Any

, instead of Union{Missing,Any} too?

It doesn’t have Union{Missing,Any} it has Union{Missing, String}.

1 Like

I see, thanks! Got it now.