What is the difference between Array{Union{Nothing, <:Real}}
and Array{<:Union{Nothing, Real}}
?
I expect Vector{Int64}
to be a subtype of the array of the union of nothing and subtypes of real, but only the latter gives true. What is the meaning of <:Union{Nothing, Real}
?
Array{<:Union{Nothing, Real}}
is a syntax sugar for Array{T} where {T<:Union{Nothing, Real}}
.
It’s called UnionAll
which means Union{Array{Nothing}, Array{Real}}
. See the talk linked below for more details.
Union{Array{Nothing}, Array{Real}}
doesn’t seem to include Vector{Union{Nothing, Int64}}
… or does it?
So now my understanding is that Union{Nothing, Real}
isn’t simply a union of type Nothing
and abstract type Real
, but also includes all concrete subtypes in the set. I suppose Union{Nothing, <:Real}
doesn’t mean the way I expect it to be, as in the union of type Nothing
and all of the concrete subtypes of Real
.
1 Like
julia> x = Array{T} where T<:Union{Nothing,S} where S <: Real
Array{T} where {S<:Real, T<:Union{Nothing, S}}
julia> y = Array{Union{Nothing, <:Real}}
Array{Union{Nothing, Real}}
julia> x == y
false
julia> y <: x
true
julia> x <: y
false
1 Like