lua-users home
lua-l archive

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


A simple testcase. Running it in the 5.4 interpreter (with newlines
removed) prints "closing" twice for me:

local x = setmetatable({}, { __close = function () print('closing') end })
local <toclose> copy1 = x
local <toclose> copy2 = x

— Gabriel



On Sat, Jun 15, 2019 at 5:03 PM Coda Highland <chighland@gmail.com> wrote:
>
>
>
> On Sat, Jun 15, 2019 at 1:35 PM Francisco Olarte <folarte@peoplecall.com> wrote:
>>
>> One question regarding to close, just seeking confirmation ( or
>> denegation in case I've read the manual wrong ).
>>
>> <toclose> depends on the variable going out of scope, so if I do
>> something like this ( I know example is stupid but something similar
>> may happen in more complex ways ):
>>
>> do
>>   local original = generate_value_with_toclose_metatmethod()
>>   ...
>>   local <toclose> copy1 = original
>>   ...
>>   local <toclose> copy2 = original
>>   ...
>> end --
>>
>> Will __toclose be called twice on the valuewhen reaching end ? ( I
>> expect it to be )
>>
>> Same question with
>> do
>>    local <toclose> o = gvwtcmm()
>>    ...
>>    do
>>       local <toclose> c1 = o
>>       ...
>>    end
>>    ...
>> end
>>
>> Will __toclose be called on reaching each end?
>>
>> ( 1st case is weird, but some code generation / value shuffling /
>> aliasing may lead to something like that, 2nd one may be more common
>> when reusing code via functions ( i.e., a function which does
>> something on an open connection closing it at end, a second function
>> which reads and acts upon some headers and dispatchs to the 1st one )
>> )
>>
>> ( How many times is called is not a problem to me, AAMOF I normally
>> dessign my "closeable" classes with an idempotent close() method, and
>> my plan to use <toclose> is just to alias it as __toclose, I just want
>> to know if I've interpreted the work manual correctly )
>>
>> Regards.
>>    Francisco Olarte.
>>
>
> Based on the contract of <toclose> and it having been reiterated several times that it applies to the variable, not to the value, I think it's safe to assume that it will fire the __close metamethod twice in the first example and at the end of both scopes in the second example.
>
> /s/ Adam