# How to define an array to contain Union{Missing, T}?

**URL:** <https://discourse.julialang.org/t/how-to-define-an-array-to-contain-union-missing-t/69776>\
**Category:** New to Julia\
**Tags:** parametric-types\
**Created:** [October 14, 2021, 9:05pm UTC](https://discourse.julialang.org/t/how-to-define-an-array-to-contain-union-missing-t/69776 "2021-10-14T21:05:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [October 14, 2021, 9:05pm UTC](https://discourse.julialang.org/t/how-to-define-an-array-to-contain-union-missing-t/69776/1 "2021-10-14T21:05:07Z")

</div>

Is there a better way to write this function using parametric types? I’m afraid my version isn’t type stable.

```julia
function f(a::AbstractArray)
    b::Vector{Union{eltype(a),Missing}} = [a[1]]
    push!(b, missing)
    return b
end

```

```julia
julia> f([1,2,3])
2-element Vector{Union{Missing, Int64}}:
 1
  missing

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 14, 2021, 9:21pm UTC](https://discourse.julialang.org/t/how-to-define-an-array-to-contain-union-missing-t/69776/2 "2021-10-14T21:21:01Z")

</div>

just

```julia
b = Vector{Union{eltype(a),Missing}}
reutrn b()

```

?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 14, 2021, 9:21pm UTC](https://discourse.julialang.org/t/how-to-define-an-array-to-contain-union-missing-t/69776/3 "2021-10-14T21:21:28Z")

</div>

```julia
function f (a::AbstractVector{T}) where {T}
    b = Union{Missing, T}[]
    push!(b, missing)
    return b
end

julia> f([1,2])
1-element Vector{Union{Missing, Int64}}:
 missing

```

if you want to keep the contents of `a`:

```julia
function f(a::AbstractVector{T}) where {T}
    b = Union{Missing, T}[]
    append!(b, a)
    return b
end

julia> f([1,2])
2-element Vector{Union{Missing, Int64}}:
 1
 2

```

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [October 14, 2021, 9:26pm UTC](https://discourse.julialang.org/t/how-to-define-an-array-to-contain-union-missing-t/69776/4 "2021-10-14T21:26:01Z")

</div>

Thanks, I was missing the `where {T}`.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 15, 2021, 12:47am UTC](https://discourse.julialang.org/t/how-to-define-an-array-to-contain-union-missing-t/69776/5 "2021-10-15T00:47:54Z")

</div>

`allowmissing(x::AbstractArray)` in `Missings.jl` is also useful for this.
