Similar things have been discussed in other threads, but this particular example was surprising to me:
julia> function main(x)
g(x,y) = x + y
f(x,inner_g) = inner_g(x)
y = 1
f(x, x -> g(x,y))
y = 2
f(x, x -> g(x,y))
end
main (generic function with 1 method)
julia> @code_warntype main(1)
Variables
#self#::Core.Const(main)
x::Int64
#84::var"#84#88"{var"#g#85"}
#83::var"#83#87"{var"#g#85"}
y::Core.Box
f::var"#f#86"
g::var"#g#85"
Body::Any
That raises the problem of not being able to use the same function twice inside a function without changing the variable names, which is quite strange (changing the second y
to z
solves the instability).
The type of y
has not even changed, and the function becomes type-unstable. Is this this issue as well? performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub
From this example: performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub I guess that the issue is the same.