# Function with vector of Union-Type

**URL:** <https://discourse.julialang.org/t/function-with-vector-of-union-type/88850>\
**Category:** General Usage\
**Tags:** function, value-types, missing-values, union\
**Created:** [October 17, 2022, 1:36pm UTC](https://discourse.julialang.org/t/function-with-vector-of-union-type/88850 "2022-10-17T13:36:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [October 17, 2022, 1:36pm UTC](https://discourse.julialang.org/t/function-with-vector-of-union-type/88850/1 "2022-10-17T13:36:14Z")

</div>

This does work:

```julia
function _foo1(_range::AbstractVector=[1,2])
    println(_range)
end

_foo1([1, missing])

```

This fails:

```julia
function _foo2(_range::Vector{Union{<:Number, Missing}}=[1, 2])
    println(_range)
end
_foo2([1, missing])

```

I do not understand why the second variant fails with the message:

```julia
ERROR: MethodError: no method matching _foo2(::Vector{Union{Missing, Int64}})
Closest candidates are:
  _foo2() at c:\data\git_repos\hycenta_stefan_pofahl\projects\Calibration\dbg_scripts\analyse_phase_sweep_dbg_output.jl:120
  _foo2(::Vector{Union{Missing, Number}}) at c:\data\git_repos\hycenta_stefan_pofahl\projects\Calibration\dbg_scripts\analyse_phase_sweep_dbg_output.jl:120

```

---

<div class="post-metadata">

**Author:** ![brainandforce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brainandforce/32/211054_2.png) [@brainandforce](https://discourse.julialang.org/u/brainandforce)\
**Post date:** [October 17, 2022, 1:42pm UTC](https://discourse.julialang.org/t/function-with-vector-of-union-type/88850/2 "2022-10-17T13:42:27Z")

</div>

I think it’s due to the invariance of Julia type parameters: `Vector{Union{<:Number,Missing}}` is not a `Vector{Missing,Int64}`. You probably want the type signature of `_foo2()` to be:

```julia
function _foo2(_range::Vector{<:Union{<:Number,Missing}})

```

---

<div class="post-metadata">

**Author:** ![ysh](https://avatars.discourse-cdn.com/v4/letter/y/df788c/32.png) [@ysh](https://discourse.julialang.org/u/ysh)\
**Post date:** [October 17, 2022, 3:05pm UTC](https://discourse.julialang.org/t/function-with-vector-of-union-type/88850/3 "2022-10-17T15:05:56Z")

</div>

To extend @brainandforce’s answer, your approach does not work because

```julia
julia> Vector{Union{Missing, Int}} <: Vector{Union{Missing, <:Number}}
false

```

(note that `Vector{Union{Missing, <:Number}` is a shorthand for `Vector{Union{Missing, T} where T<:Number}`)  
However,

```julia
julia> Vector{Union{Missing, Int}} <: Vector{Union{Missing, T}} where T <: Number
true

```

Therefore, you can define your function as

```julia
function _foo2(_range::Vector{Union{T, Missing}}=[1, 2]) where T <: Number

```

See also [https://docs.julialang.org/en/v1/manual/types/#UnionAll-Types](https://docs.julialang.org/en/v1/manual/types/#UnionAll-Types)

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [October 17, 2022, 3:13pm UTC](https://discourse.julialang.org/t/function-with-vector-of-union-type/88850/4 "2022-10-17T15:13:16Z")

</div>

> [@ysh](#):
>
> `function _foo2(_range::Vector{Union{T, Missing}}=[1, 2]) where T <: Number`

This does not work for me. I have to add `<:` befor `Union{}`:

```julia
function _foo_B(_range::Vector{Union{T, Missing}}=[1, 2]) where T <: Number
    println(_range)
end
_foo_B()

```

Error:

```julia
ERROR: MethodError: no method matching _MyLibFunctionWithUnionType_B(::Vector{Int64})

```

---

<div class="post-metadata">

**Author:** ![ysh](https://avatars.discourse-cdn.com/v4/letter/y/df788c/32.png) [@ysh](https://discourse.julialang.org/u/ysh)\
**Post date:** [October 17, 2022, 3:43pm UTC](https://discourse.julialang.org/t/function-with-vector-of-union-type/88850/5 "2022-10-17T15:43:57Z")

</div>

Sorry, my bad. Indeed, my approach will only work if at least one element of `_range` is `missing`. Otherwise it won’t work because e.g.

```julia
julia> Vector{Int} <: Vector{Union{Missing, T}} where T <: Number
false

```

even though

```julia
julia> Int <: Union{Missing, T} where T <: Number
true

```
