lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Thu, Aug 8, 2019 at 1:01 PM Philippe Verdy <> wrote:
This looks like a bug: even if the functions are declared multiple times (in a loop), they are all in the same context (same closure), and should resolve as one, because even their upvalues will be necessarily the same; here the only thing that changes is the current value of the outer variable j=1,2, whose value if not even used in the function and not even part of its closure (if j was part of the closure and used like " function(x) return 2*x+j end  " the value of j would be kept in that closure in upvalues even after the foor loop exits and the local vairavle j no longer exists: that variable would remain in the closure, and in that case the closure should be duplicated creating two separate functions). 

If a closure is created in a loop but it does not depend on any local variables inside the loop you can move it out of the loop, and avoid the overhead of creating different but equivalent closures each time. If it does depend on variables declared inside the loop it's a different closure, because it refers to a different instance of the local variable each time you go through the loop.

Either way the cache that is used in Lua 5.2 and 5.3 to reuse a closure is probably.