# Preventing type instability from union nothing

**URL:** https://discourse.julialang.org/t/preventing-type-instability-from-union-nothing/46168
**Category:** New to Julia
**Tags:** question, parametric-types
**Created:** [September 7, 2020, 12:11am UTC](https://discourse.julialang.org/t/preventing-type-instability-from-union-nothing/46168 "2020-09-07T00:11:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)
#### Post date: [September 7, 2020, 12:11am UTC](https://discourse.julialang.org/t/preventing-type-instability-from-union-nothing/46168/1 "2020-09-07T00:11:44Z")

</div>

I’m trying to understand parametric types and parametric methods with a simple example.  
I have some discrete data `w` with some metadata `t`.  
All I want to show is how many times different metadata values pop up for various values of `w`.

```julia
struct myunit{W<:Integer,T<:Integer}
  w::W
  t::T
end

function getNWK(d::AbstractArray{T},l::AbstractArray{U},k::Integer) where {T<:myunit,U<:Integer}
  nwk = zeros(k,length(l))
  for i in 1:length(d)
    nwk[d[i].t,d[i].w] += 1
  end
  nwk
end

```

> **@code\_warntype**
>
> ```julia
> l = [1:100;]
> x = rand(1:100,1000)
> y = rand(1:3,1000)
> arr = myunit.(x,y)
> @code_warntype getNWK(arr,l)
> 
> Variables
> #self#::Core.Compiler.Const(getNWK, false)
> d::Array{myunit{Int64,Int64},1}
> l::Array{Int64,1}
> nwk::Array{Float64,1}
> @_5::Union{Nothing, Tuple{Int64,Int64}}
> i::Int64
> 
> Body::Array{Float64,1}
> 1 ─ %1 = Main.length(l)::Int64
> │ (nwk = Main.zeros(%1))
> │ %3 = Main.length(d)::Int64
> │ %4 = (1:%3)::Core.Compiler.PartialStruct(UnitRange{Int64}, Any[Core.Compiler.Const(1, false), Int64])
> │ (@_5 = Base.iterate(%4))
> │ %6 = (@_5 === nothing)::Bool
> │ %7 = Base.not_int(%6)::Bool
> └── goto #4 if not %7
> 2 ┄ %9 = @_5::Tuple{Int64,Int64}::Tuple{Int64,Int64}
> │ (i = Core.getfield(%9, 1))
> │ %11 = Core.getfield(%9, 2)::Int64
> │ %12 = Base.getindex(d, i)::myunit{Int64,Int64}
> │ %13 = Base.getproperty(%12, :t)::Int64
> │ %14 = Base.getindex(d, i)::myunit{Int64,Int64}
> │ %15 = Base.getproperty(%14, :w)::Int64
> │ %16 = Base.getindex(nwk, %13, %15)::Float64
> │ %17 = (%16 + 1)::Float64
> │ Base.setindex!(nwk, %17, %13, %15)
> │ (@_5 = Base.iterate(%4, %11))
> │ %20 = (@_5 === nothing)::Bool
> │ %21 = Base.not_int(%20)::Bool
> └── goto #4 if not %21
> 3 ─ goto #2
> 4 ┄ return nwk
> 
> ```

Line 5 of the @code\_warntype output was highlighted in yellow:

```julia
  @_5::Union{Nothing, Tuple{Int64,Int64}}

```

I’m still getting the hang of @code\_warntype but it appears to be concerned that if `k` is 0 then the output array will have no rows, how do I tell the compiler that this will never happen?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 7, 2020, 12:59am UTC](https://discourse.julialang.org/t/preventing-type-instability-from-union-nothing/46168/2 "2020-09-07T00:59:59Z")

</div>

> [@mkarikom](#):
>
> I’m still getting the hang of @code\_warntype but it appears to be concerned that if `k` is 0 then the output array will have no rows, how do I tell the compiler that this will never happen?

The compiler splits unions of two so it’s perfect fine. That comes from the iterator.

---

<div class="post-metadata">

### Author: ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)
#### Post date: [September 7, 2020, 4:06am UTC](https://discourse.julialang.org/t/preventing-type-instability-from-union-nothing/46168/3 "2020-09-07T04:06:15Z")

</div>

perfect, thanks!
