I find
julia> methods(Union{})
# 4 methods for callable object:
[1] (::Type{T})(x::Number) where T<:AbstractChar in Base at char.jl:48
[2] (::Type{T})(x::Rational{S}) where {S, T<:AbstractFloat} in Base at rational.jl:114
[3] (::Type{T})(x::Real, r::RoundingMode) where T<:AbstractFloat in Base.Rounding at rounding.jl:207
[4] Union{}(a...) in Core at boot.jl:263
I realize that this is somewhat unusual, and may be meaningless, but what are the methods telling me here? In particular, why the top 3 methods, and no other?
julia> Union{} <: T where T
true
so, T
can be anything really, can’t it?
The first three says that you can do something like Int(4.0)
which is equivalent to (Int)(4.0)
. In this case, Int
will be the ::Type{T}
for the third method and 4.0
will be x::Real
.
As Union{}
is the bottom type (more on this below), it means where T <: AnotherType
will always be true, and that is why it is shown here.
The fourth method is just to throw an error if you try to instantiate an empty union Union{}
. The code is here.
Finally, yes. Union{}
is the bottom type of Julia type hierarchy, which means that it is a subtype of all types, including itself, just like Any
is the top type, i.e., the supertype of every all types, including itself. See
julia> Union{} <: Union{}
true
julia> Any <: Any
true
julia> T where T # this is just an alias for Any
Any