# How to dispatch on an array of T or Union{Nothing,T} but not nothing

**URL:** <https://discourse.julialang.org/t/how-to-dispatch-on-an-array-of-t-or-union-nothing-t-but-not-nothing/19504>\
**Category:** General Usage\
**Created:** [January 11, 2019, 4:15am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-array-of-t-or-union-nothing-t-but-not-nothing/19504 "2019-01-11T04:15:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndinsmore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndinsmore/32/7433_2.png) [@ndinsmore](https://discourse.julialang.org/u/ndinsmore)\
**Post date:** [January 11, 2019, 4:15am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-array-of-t-or-union-nothing-t-but-not-nothing/19504/1 "2019-01-11T04:15:42Z")

</div>

I am looking for a concise way to match an `AbstractArray` of T or `Union{Nothing,T}` but not `Nothing`. Does anyone have any ideas how to do that?

The general test I have been using is the following, with the ideal result being [true,true,false]:

```julia
julia> test_cases=[AbstractArray{Union{Nothing,Float64},<:Any},
                   AbstractArray{Float64,<:Any},
                   AbstractArray{Nothing,<:Any} ];

julia> typetest(T3)=((T1,T2)->T1<:T2).(test_cases,[T3]);

julia> typetest(AbstractArray{Union{Float64,Union{Nothing,Float64}},<:Any})
3-element BitArray{1}:
  true
 false
 false

julia> typetest(AbstractArray{T,<:Any} where T<:Union{Float64,Union{Nothing,Float64}})
3-element BitArray{1}:
 true
 true
 true

julia> typetest(AbstractArray{T,<:Any} where T<:Union{Nothing,Float64})
3-element BitArray{1}:
 true
 true
 true

julia> typetest(Union{AbstractArray{Float64,<:},AbstractArray{Union{Nothing,Float64}},<:Any})
3-element BitArray{1}:
 true
 true
 true

```

I think that the first case might be a bug, does anyone have any idea why it gives that result?

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [January 11, 2019, 5:16am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-array-of-t-or-union-nothing-t-but-not-nothing/19504/2 "2019-01-11T05:16:24Z")

</div>

> [@ndinsmore](#):
>
> I am looking for a concise way to match an `AbstractArray` of T or `Union{Nothing,T}` but not `Nothing`

```julia
f(::AbstractArray{T,<:Any}) where T = println("$T only")
f(::AbstractArray{Union{T, Nothing},<:Any}) where T = println("$T and Nothing")
f(::AbstractArray{Nothing,<:Any}) = println("Nothing only")

```

```julia
julia> f([1,2,3])
Int64 only

julia> f([1, nothing, 3])
Int64 and Nothing

julia> f([nothing, nothing, nothing])
Nothing only

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 11, 2019, 8:51am UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-array-of-t-or-union-nothing-t-but-not-nothing/19504/3 "2019-01-11T08:51:32Z")

</div>

You don’t need `,<:Any`

---

<div class="post-metadata">

**Author:** ![ndinsmore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndinsmore/32/7433_2.png) [@ndinsmore](https://discourse.julialang.org/u/ndinsmore)\
**Post date:** [January 11, 2019, 1:26pm UTC](https://discourse.julialang.org/t/how-to-dispatch-on-an-array-of-t-or-union-nothing-t-but-not-nothing/19504/4 "2019-01-11T13:26:14Z")

</div>

So the signature that matches is:`AbstractArray{Union{Float64,T}} where T<:Nothing`…matching `AbstractArray{Float64}` , `AbstractArray{Union{Float64,Nothing}}`, but not `AbstractArray{Nothing}`.

But could someone explain why that matches the first two cases?  
While `AbstractArray{T} where T<:Union{Float64,Nothing}` matches all three cases?  
And `AbstractArray{Union{Float64,Nothing}} ` only matches the first one?
