# Switching data type in a vector

**URL:** <https://discourse.julialang.org/t/switching-data-type-in-a-vector/89423>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 28, 2022, 4:05am UTC](https://discourse.julialang.org/t/switching-data-type-in-a-vector/89423 "2022-10-28T04:05:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phuntsho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phuntsho/32/46546_2.png) [@Phuntsho](https://discourse.julialang.org/u/Phuntsho)\
**Post date:** [October 28, 2022, 4:06am UTC](https://discourse.julialang.org/t/switching-data-type-in-a-vector/89423/1 "2022-10-28T04:06:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [October 28, 2022, 5:34am UTC](https://discourse.julialang.org/t/switching-data-type-in-a-vector/89423/2 "2022-10-28T05:34:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 28, 2022, 7:08am UTC](https://discourse.julialang.org/t/switching-data-type-in-a-vector/89423/3 "2022-10-28T07:08:33Z")

</div>

> [@Phuntsho](#):
>
> I don’t want to make it V::Vector{Tuple{Int64, Float64}} or V::Vector{Tuple{Int64, Any}} for performance.

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
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)

```
