[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [NoW] Deja vu
- From: Tim Hill <drtimhill@...>
- Date: Fri, 9 Aug 2019 15:42:02 -0700
> On Aug 8, 2019, at 2:06 PM, Soni They/Them L. <fakedme@gmail.com> wrote:
>
>
>
> On 2019-08-07 8:28 p.m., Egor Skriptunoff wrote:
>> 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?
>
> Lua 5.4 has const locals in loops. Each loop iteration gets a new j, which is const.
>
> That just means each closure is using a different upvalue, and gets closed on each loop iteration. Try calling them!
>
> (Well, I could be wrong, I guess. Correct me if I'm wrong.)
>
The const-ness is new, but (since at least 5.2) Lua has always treated a for loop iterator variable as newly declared for each loop iteration, so each instance of the closure would get a distinct “j” (though, of course, in this case the inner function does not close j, so the OP issue is still a regression).
—Tim