Switching data type in a vector

I have a vector V::Vector{Tuple{Int64, Int64}} but sometimes one of the second entries becomes Float64. I don’t want to make it V::Vector{Tuple{Int64, Float64}} or V::Vector{Tuple{Int64, Any}} for performance. Is there a way to handle this such that V is the vector of Tuples with Int64 when all entries are Int64 but when the second entry is float64 V becomes Vector{Tuple{Int64, Float64}} to accept the float64? Thank you in advance.

The question is a bit unclear. You have an array V = Tuple{Int,Int}[], but want it to change type if you do something like push!(V, (1, 2.3)) or V[i] = (1, 2.3)? Or do you want a function which sometimes returns a Tuple{Int, Int} vector and sometimes Tuple{Int, Float64} vector depending on something?

The first thing won’t work, the second is possible. It all depends on your concrete problem.


Firstly, why can’t you make it Vector{Tuple{Int, Float64}}? I cannot see why that should affect performance. If the values you want to store are sometimes non-integer valued, then you should be using Tuple{Int, Float64}.

As for converting, you can use reinterpret, but be aware that this will change the numerical values of the second tuple member:

julia> V = [(2, 3), (9, 5)]
2-element Vector{Tuple{Int64, Int64}}:
 (2, 3)
 (9, 5)

julia> W = reinterpret(Tuple{Int, Float64}, V)
2-element reinterpret(Tuple{Int64, Float64}, ::Vector{Tuple{Int64, Int64}}):
 (2, 1.5e-323)
 (9, 2.5e-323)