# Widening the element type of an array?

**URL:** <https://discourse.julialang.org/t/widening-the-element-type-of-an-array/106085>\
**Category:** General Usage\
**Tags:** type, arrays\
**Created:** [November 11, 2023, 6:06pm UTC](https://discourse.julialang.org/t/widening-the-element-type-of-an-array/106085 "2023-11-11T18:06:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [November 11, 2023, 6:06pm UTC](https://discourse.julialang.org/t/widening-the-element-type-of-an-array/106085/1 "2023-11-11T18:06:09Z")

</div>

This may be a rather unusual question but: I’d like to “widen” the element type of a given Array to an abstract type, such that the resulting array contains the same data as the original, but can store values of another type in some cells (obviously this is not in a critical part of my code, performance-wise).

Like this:

```julia-repl
julia> my_widen(arr, typ) = copy!(similar(arr, typ), arr)

julia> a = [1, 2, 3];

julia> b = my_widen(a, Number); b[2] = 2.0; b
3-element Vector{Number}:
 1
 2.0
 3

julia> c = my_widen(a, Union{Int, Nothing}); c[2] = nothing; c
3-element Vector{Union{Nothing, Int64}}:
 1
  nothing
 3

```

Does something like this exist already?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 11, 2023, 6:10pm UTC](https://discourse.julialang.org/t/widening-the-element-type-of-an-array/106085/2 "2023-11-11T18:10:24Z")

</div>

> [@ffevotte](#):
>
> Does something like this exist already?

```julia
julia> a = [1, 2, 3];

julia> Vector{Number}(a)
3-element Vector{Number}:
 1
 2
 3

```

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [November 11, 2023, 6:11pm UTC](https://discourse.julialang.org/t/widening-the-element-type-of-an-array/106085/3 "2023-11-11T18:11:26Z")

</div>

Of course! How could I not think of trying this? Thanks a lot
