[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Query about to-be-closed semantics
- From: Arnaud Delobelle <arnodel@...>
- Date: Fri, 14 Jan 2022 09:48:45 +0000
Hi all
My name is Arnaud Delobelle - I have been using Lua for about 10 years
but this is my first post on this list!
I am looking at Lua 5.4 and have some queries about the new
"to-be-closed" variables.
Here is an example program
---- tbc.lua ----
local meta = {__close = function() print"123" end}
local t = {}
setmetatable(t, meta)
local function f()
local c <close> = t
print"f"
setmetatable(t, nil)
end
f()
---- end ----
Running this with Lua 5.4.3 I get the following output:
----
f
lua: tbc.lua:8: metamethod 'close' is not callable (a nil value)
stack traceback:
tbc.lua:8: in local 'f'
tbc.lua:9: in main chunk
[C]: in ?
----
That makes sense as I removed the metatable from t before "closing
time". However, when defining c
local c <close> = t
at that point the Lua runtime checks that t indeed has a close
metamethod and produces an error if that's not the case. Now it feels
to me that this check is conflicting with the behaviour above as this
check is not so meaningful if what it checks can be subsequently
invalidated.
Has this been given consideration? For example the runtime could
remember the close metamethod at variable definition so it will
definitely be the one used when the to-be-closed variable comes out of
scope.
Regards,
Arnaud Delobelle