Vector of a union of two different numeric types---Vector{Union{Int64, Float64}}

Dear Julia Experts, please, what’s wrong with the following? Can’t I make a vector that is a union of two different numerical types, i.e. a vector of either Int64 or Float64 elements?

IntFloat = Union{Int64, Float64}
function fun(x::Vector{IntFloat})
    println(x)
end
fun(Int64[1, 2, 3])
fun(Float64[1, 2, 3])

Type parameters are invariant, see Types · The Julia Language

You need to define fun as:

function fun(x::Vector{T}) where {T<:Union{Float64,Int64}}
    println(x)
end
2 Likes

thank you :slight_smile: