So, when [1,2,3,"hello"]
is encountered, it’s (more or less) lowered to Base.vect(1,2,3,"hello")
. Base.vect
will construct a vector, it needs an element type. The element type is found with Base.promote_typeof(1,2,3,"hello")
, which returns “Any” (essentially, promote_type(Int,Int,Int,String)
).
It can be changed, but that’s type piracy if none of the types are your own.
julia> Base.promote_rule(::Type{Int}, ::Type{String}) = Union{Int,String}
julia> [1,2,"foo"]
3-element Vector{Union{Int64, String}}:
1
2
"foo"
julia> Base.promote_rule(::Type{Int}, ::Type{String}) = Union{Integer,String}
julia> [1,2,"foo"]
3-element Vector{Union{Integer, String}}:
1
2
"foo"
julia> [1,2.0,"foo"]
3-element Vector{Any}:
1
2.0
"foo"
It’s of course much safer to create your vector as Union{Int,String}[1,2,3,"hello"]
.