julia> function closureB(x::Int64)::Function
### the only difference with closureA()
if x < 0
error("abc")
end
### the only difference with closureB()
oneto3 = Base.OneTo(3)
local f
let oneto3 = oneto3
function f()
for i in oneto3
println(i)
end
end
end
return f
end
closureB (generic function with 1 method)
julia> gg = closureB(1)
(::f) (generic function with 1 method)
julia> @code_warntype gg()
Variables:
#self#::#f#6{Base.OneTo{Int64}}
i::Int64
#temp#::Int64
Body:
begin
SSAValue(0) = (Core.getfield)(#self#::#f#6{Base.OneTo{Int64}}, Symbol("#4#oneto3"))::Base.OneTo{Int64}
#temp#::Int64 = 1
3:
unless (Base.not_int)((#temp#::Int64 === (Base.add_int)((Core.getfield)(SSAValue(0), :stop)::Int64, 1)::Int64)::Bool)::Bool goto 18
SSAValue(3) = #temp#::Int64
SSAValue(4) = (Base.add_int)(#temp#::Int64, 1)::Int64
i::Int64 = SSAValue(3)
#temp#::Int64 = SSAValue(4) # line 13:
$(Expr(:inbounds, false))
# meta: location coreio.jl println 5
SSAValue(2) = (Core.typeassert)(Base.STDOUT, Base.IO)::IO
# meta: pop location
$(Expr(:inbounds, :pop))
(Base.print)(SSAValue(2), i::Int64, $(QuoteNode('\n')))::Void
16:
goto 3
18:
return
end::Void
1 Like