# How to dispatch on typed vectors

**URL:** <https://discourse.julialang.org/t/how-to-dispatch-on-typed-vectors/60633>\
**Category:** General Usage\
**Tags:** dispatch\
**Created:** [May 6, 2021, 9:25am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-typed-vectors/60633 "2021-05-06T09:25:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@florian](https://discourse.julialang.org/u/florian)\
**Post date:** [May 6, 2021, 9:25am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-typed-vectors/60633/1 "2021-05-06T09:25:19Z")

</div>

How can I write a function that works on `Vector{T1}` as well as `Vector{Union{T1, T2}}`?

So for example I want the function to work on `["hello", "world", missing]` as well as `[“hello”, “world”].  
I can’t dispatch to the individual types, the vector is important.

Thanks a lot in advance!

---

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [May 6, 2021, 9:32am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-typed-vectors/60633/2 "2021-05-06T09:32:18Z")

</div>

```julia
f(v::Vector{T}) where T<:Union{String,Missing} = typeof(v)

```

or the shorthand

```julia
f(v::Vector{<:Union{String,Missing}}) = typeof(v)

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [May 6, 2021, 9:42am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-typed-vectors/60633/3 "2021-05-06T09:42:00Z")

</div>

Note that this will also include `Vector{Missing}` (as well as `Vector{Union{}}` FWIW). If you don’t want that, you could specify the type as:

```julia
f(v::Vector{T}) where String<:T<:Union{String,Missing} = typeof(v)

```

which will only allow `Vector{String}` and `Vector{Union{String, Missing}}`.

---

<div class="post-metadata">

**Author:** ![florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@florian](https://discourse.julialang.org/u/florian)\
**Post date:** [May 6, 2021, 9:48am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-typed-vectors/60633/4 "2021-05-06T09:48:05Z")

</div>

Thanks a lot for that hint, this is exactly what I was looking for!

---

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [May 6, 2021, 10:20am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-typed-vectors/60633/5 "2021-05-06T10:20:13Z")

</div>

Interesting 🤔 Could you elaborate a bit what `String<:T<:Union{String,Missing}` means in this context?  
And another question for my education: Would one actually encounter a `Vector{Union{}}` in the wild? I have only been able to deliberately construct one by doing `typeof(Union{}[]) = Vector{Union{}}` because on the other hand `typeof([Union{}]) = Vector{Core.TypeofBottom}`.

Finally, I would probably not exclude `Vector{Missing}` because if I was expecting a `Vector{T,Missing}` there is the possibility that all values are missing in which case type inference might return the former type. Unless you want a specific method for that case but then defining `f(::Vector{Missing})` would still work because it is more specific, no?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [May 6, 2021, 10:30am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-typed-vectors/60633/6 "2021-05-06T10:30:35Z")

</div>

> [@helgee](#):
>
> Interesting 🤔 Could you elaborate a bit what `String<:T<:Union{String,Missing}` means in this context?

It just means that `T` must be a supertype of `String`, as well as a subtype of `Union{String,Missing}`. This excludes `Missing` since it’s not a supertype of `String`.

> [@helgee](#):
>
> Would one actually encounter a `Vector{Union{}}` in the wild? I have only been able to deliberately construct one by doing `typeof(Union{}[]) = Vector{Union{}}` because on the other hand `typeof([Union{}]) = Vector{Core.TypeofBottom}` .

Yes, an empty vector with element type `Union{}` is sometimes useful. BangBang.jl for example uses it as a neutral element for reductions producing vectors, because `promote_type(Union{}, T)` is always just `T` (see [Home · BangBang.jl](https://juliafolds.github.io/BangBang.jl/dev/#BangBang.NoBang.Empty)). You can never have any defined elements in a `Vector{Union{}}` though, since the defining property of the bottom type is that it can not have any instances.
