Can you explain how I should understand?
Ignoring the Vector
part first, Union{Missing, <:Real}
is an iterated union of unions. It is equal (==
) to the full form Union{Missing, T} where T <: Real
and the simplified Union{Missing, Real}
. That’s just how unions of unions simplify to a union, and sometimes Julia will do that simplification automatically so watch out for apparent discrepancies like this:
julia> Union{Union{Missing, <:Real}, Integer}
Union{Missing, Real}
Vector{Union{Missing, Real}}
is a concrete type with an abstract type parameter, and a concrete type cannot be subtyped. Vector{Union{Missing, T}} where T <: Real
is an iterated union of an infinite number of concrete Vector
types, including Vector{Union{Missing, Int64}}
and Vector{Union{Missing, Real}}
. Iterated unions are abstract types, and abstract types can be subtyped.
<:Union{Missing,Real}
also works. Documentation should have some particular explanation for this type of use.
I don’t think the equivalency of unions of unions to a simpler union is documented, and it’s hard to find a good way to explain that. In simpler cases it’s like set theory, but not everything is that simple. In practice, it’s more readable to write down flat unions instead of nesting them.
The inability of concrete types to subtype each other is documented in the introduction of the Types section of the Manual, and the invariance of type parameters (in short, T<:U
does not imply V{T}<:V{U}
) and superficial covariance of iterated unions (T<:U
does imply V{T} <: V{<:U}
) is documented in the Parametric Types subsection.
It also occurs to me that you might have had a wrong expectation of the V{<:U}
shorthand for V{X} where {X<:U}
. Visually, if you just placed the where clause into the curly braces, you might have thought
Vector{Union{Missing, T}} where T <: Real
becomes
Vector{Union{Missing, <:Real}}
but the shorthand can only work across 1 layer of curly braces, not 2. This attempt made an entirely different type, which we can see if we go back the other way across 1 layer of curly braces:
Vector{Union{Missing, U} where U <: Real}
As far as I know, there is no shorthand version of the first type. That’s why the shorthand is only a shorthand, not the primary syntax.
So you mean:
Vector{Union{Missing,T} where T <: Real} == Vector{Union{Missing,<:Real}}
and it seems that:
Vector{Union{Missing,Real} == Vector{Union{Missing,<:Real}}
so as you said, the shorthand is not working with second curly braces. It is good to be aware of this specs.
Thank you for the long…
TLDR:
Union{Missing, <:Real}
is shorthand that creates a newUnionAll
with an anonymous type variable, and it’s equal toUnion{Missing, T} where {T <: Real}
.Union{Missing, T} where {T <: Real}
is simplified by Julia intoUnion{Missing, Real}
, becauseUnion
is covariant, likeTuple
.
But not working for parametric types like Arrays. Must use the “type variable” and not the shorthand <:Type for covariation.
because most of the time, the types are invariant, which gives property:
julia> Vector{Int} <: Vector{Real}
false
That’s not true.
julia> Vector{<:Real} == Vector{T} where T<:Real
true
Again, the key is 1 level of curly braces. The simplification of unions of unions is a completely separate matter, we can remake the shorthand usage example without that complication:
julia> Vector{Vector{Int}} <: Vector{Vector{<:Real}}
false
julia> Vector{Vector{Int}} <: Vector{Vector{T}} where T<:Real
true
which has the same explanation:
julia> isconcretetype(Vector{Vector{<:Real}})
true
julia> Vector{Vector{<:Real}} == Vector{Vector{U} where U<:Real}
true
julia> (Vector{Vector{U} where U<:Real}) == (Vector{Vector{T}} where T<:Real)
false
julia> Vector{Vector{Int}} <: Vector{Vector{<:Real}}
false
julia> Vector{Vector{Int}} <: Vector{Vector{T}} where T<:Real
true
I thought that covariation should deliver true , but with the shorthand is giving false, so it is staying invariant, isn’t it?
Again, Vector{Vector{<:Real}}
is not a shorthand for Vector{Vector{T}} where T<:Real
, the shorthand only works across 1 layer of curly braces. See my preceding comment for the type it is a shorthand for. Covariance and invariance are red herrings here.