Why [1, 2, 3] is not a Vector{Number}?

It is all about whether the container (or container of containers) holds abstract types (versatile but slow) or concrete types (restrictive but fast).

v1 = [1, 2]      # 2-element Vector{Int64}
v2 = [1.1, 2.2]  # 2-element Vector{Float64}
vv1 = Vector{<:Number}[]  # 0-element Vector{Vector{<:Number}}
push!(vv1, v1)            # 1-element Vector{Vector{<:Number}}
push!(vv1, v2)            # 2-element Vector{Vector{<:Number}}
vv2 = Vector{Int64}[]  # 0-element Vector{Vector{Int64}}
push!(vv2, v1)         # 1-element Vector{Vector{Int64}}
push!(vv2, v2)  # Error: vv2 container can only hold Vector{Int64} elements
vv1 isa Vector{Vector{<:Number}}   # true
vv2 isa Vector{Vector{<:Number}}   # false
vv1 isa Vector{<:Vector{<:Number}} # true
vv2 isa Vector{<:Vector{<:Number}} # true

I asked a related question here which may also be useful to you.

2 Likes