Scope and anonymous functions

https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope

the behavior should be explained here, My guess is your first function should fall into:

When x = <value>

  1. Existing Local

because function is a hard local scope and your assignment is in a soft local scope (for).


One way to make your first function to “behave” is by forcing a local variable:

function a()
               funcs = []
               i = 1
               for j=1:100
                  local k=i
                  push!(funcs, (args...)->k)
                  i += 1
               end
              return funcs
       end
1 Like