lua-users home
lua-l archive

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


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). 

For me, as there's a single closure instance, the test should return true in Lua 5.4, like in Lua 5.2 and 5.3; returning to the bad Lua 5.1 behavior is a regression, it just means that we can no longer unify function declarations that are explicitly created in the same closure and not depending on other external variables.



Le jeu. 8 août 2019 à 16:25, Egor Skriptunoff <egor.skriptunoff@gmail.com> a écrit :
t = {}
for j = 1, 2 do
   t[j] = function(x) return 2*x end
end
print(t[1] == t[2])

Lua 5.1    false
Lua 5.2    true
Lua 5.3    true
Lua 5.4    false

It seems that the equality between function values has returned back to its Lua 5.1 semantics.

1)
Why "function values caching" has been removed from Lua 5.4?

2)
Could we rely on the fact that in Lua 5.4+ any created function is unique value?