lua-users home
lua-l archive

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


On Sun, 2021-09-19 at 14:28 +0200, Marc Balmer wrote:
> Are may two assumptions below correct:
> 
> - For many objects written in C, the __close and __gc metamethods can
> be the same?

Yes, but you need to take care to ensure that closed objects being
accessible won't cause issues (which is not the case for "traditional"
__gc metamethods). This includes attempting to close object twice in a
row, among other things.

> - If a <local> variable is closed (__close called), it will not be
> called by the garbagecollector?

No, as easily demonstrated:

do
local a <close> = setmetatable({}, {
    __close = function() print('closed') end;
    __gc = function() print('GCd') end;
})
end; --> closed
collectgarbage() --> GCd
-- 
v <v19930312@gmail.com>