@_4::Union{Nothing, Tuple{Int64,Int64}} in simple iterator

What that @_4::Union{Nothing, Tuple{Int64,Int64}} is indicating? Where is there any possible Tuple in this code? Is there anything important about this warning of type evaluation? When one runs @code_warntype that line is yellow, suggesting that it deserves some attention.

julia> function s(x)
         s = 0
         for val in x
           s = s + val
         end
         s
       end
s (generic function with 1 method)

julia> x = ones(Int64,3);

julia> @code_warntype s(x)
Variables
  #self#::Core.Compiler.Const(s, false)
  x::Array{Int64,1}
  s::Int64
  @_4::Union{Nothing, Tuple{Int64,Int64}}   #<<<<<<<<< THIS LINE
  val::Int64

Body::Int64
1 ─       (s = 0)
β”‚   %2  = x::Array{Int64,1}
β”‚         (@_4 = Base.iterate(%2))
β”‚   %4  = (@_4 === nothing)::Bool
β”‚   %5  = Base.not_int(%4)::Bool
└──       goto #4 if not %5
2 β”„ %7  = @_4::Tuple{Int64,Int64}::Tuple{Int64,Int64}
β”‚         (val = Core.getfield(%7, 1))
β”‚   %9  = Core.getfield(%7, 2)::Int64
β”‚         (s = s + val)
β”‚         (@_4 = Base.iterate(%2, %9))
β”‚   %12 = (@_4 === nothing)::Bool
β”‚   %13 = Base.not_int(%12)::Bool
└──       goto #4 if not %13
3 ─       goto #2
4 β”„       return s

julia> 

1 Like

A for loop lowers to calls to iterate which returns a Tuple. It’s yellow (and not red) because it likely does not require attention.

3 Likes

https://julialang.org/blog/2018/07/iterators-in-julia-0.7/#iteration_in_julia_07 describes the Union used in the iteration protocol, if you’d like to learn more.

3 Likes