Why Union{Missing, Int}==Union{Int, Missing}, while Tuple differs?

question
#1

I know it is necessary for Union to behavior like this, while the type parameter order of Tuple must batter.

But how? Is Union specially handled by the julia compiler?

0 Likes

#2

The definition of two types T and S being equal is that T<:S && S<:T. There are also some normalization for types, e.g.:

julia> a = Union{Missing, Int}
Union{Missing, Int64}

julia> b = Union{Int, Missing}
Union{Missing, Int64}

so they actually end up being represented identically.

3 Likes

#3

Yeah, think of a Union as a set of Types, i.e. unordered collection of unique items, whereas a Tuple Type is just an ordered list of Types. You can see the set behavior of Union by doing things like Union{Int, Int, Float64}, or as you’ve noticed, Union{Int, Missing} == Union{Missing, Int}.

2 Likes