# @code\_warntype, for loops and spurious Union types

**URL:** <https://discourse.julialang.org/t/code-warntype-for-loops-and-spurious-union-types/44318>\
**Category:** General Usage\
**Created:** [August 5, 2020, 10:13am UTC](https://discourse.julialang.org/t/code-warntype-for-loops-and-spurious-union-types/44318 "2020-08-05T10:13:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 5, 2020, 10:13am UTC](https://discourse.julialang.org/t/code-warntype-for-loops-and-spurious-union-types/44318/1 "2020-08-05T10:13:30Z")

</div>

If I define some function that includes a for loop and run `@code_warntype` on it:

```julia
function f()
    for i in 1:10
        i
    end
    return
end

@code_warntype f()

```

The variables listed always contain a `Union` type:

```julia
Variables
  #self#::Core.Compiler.Const(f, false)
  @_2::Union{Nothing, Tuple{Int64,Int64}}
  i::Int64

```

Whch means the function shows up as yellow/dynamic in the atom profiler, which is kinda weird.

What’s going on here?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 5, 2020, 10:38am UTC](https://discourse.julialang.org/t/code-warntype-for-loops-and-spurious-union-types/44318/2 "2020-08-05T10:38:59Z")

</div>

That’s just how `iterate` works, see

[https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration-1](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration-1)

It is not spurious, but usually innocuous: your code will be optimized.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 5, 2020, 10:43am UTC](https://discourse.julialang.org/t/code-warntype-for-loops-and-spurious-union-types/44318/3 "2020-08-05T10:43:18Z")

</div>

That makes sense. It’s still annoying in large functions where you can’t easily tell for-loop type instability from more serious kinds at a glance. Maybe Juno could ignore `Union{Nothing,Tuple{Int,Int}}`.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 5, 2020, 10:50am UTC](https://discourse.julialang.org/t/code-warntype-for-loops-and-spurious-union-types/44318/4 "2020-08-05T10:50:02Z")

</div>

> [@Raf](#):
>
> where you can’t easily tell for-loop type instability from more serious kinds at a glance

Note that in the REPL, `Union` is highlighted with a different color than other type instabilities. Eg

 ![union](https://global.discourse-cdn.com/julialang/original/3X/8/c/8c46e4ea062dd4cd4752b06c5c0435e5944f084c.png)

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [August 5, 2020, 10:52am UTC](https://discourse.julialang.org/t/code-warntype-for-loops-and-spurious-union-types/44318/5 "2020-08-05T10:52:12Z")

</div>

In the atom profiler yellow is for all dynamic dispatch, red for allocation - but for loops can show up as _either_. Which further complicates it.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 5, 2020, 11:08am UTC](https://discourse.julialang.org/t/code-warntype-for-loops-and-spurious-union-types/44318/6 "2020-08-05T11:08:02Z")

</div>

That’s an issue with Atom then I guess (note that I am not a user, so I may not be aware of some customization which fixes this).
