[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Yet another proposal for <toclose> (was Re: [ANN] Lua 5.4.0 (alpha) now available)
- From: pocomane <pocomane_7a@...>
- Date: Sat, 29 Jun 2019 07:29:01 +0200
On Sat, Jun 29, 2019 at 1:12 AM Sergey Kovalev <kovserg33@gmail.com> wrote:
>
> > The proposed 'defer' statement looks like this:
> >
> > defer ... end
> >
> > with the guarantee that f will be invoked at scope exit.
> >
I think that <toclose> is more powerful since it let you to define an
object in a scope, but close it in another one. E.g. (I already wrote
something like this in another post):
local function my_complex_object()
local result = {} -- NOT <toclose>
-- ...
return setmetatable( result, {__close = my_finalizer } )
end
do
local <toclose> obj = my_complex_object()
-- ... use obj as you wish, it will be close at end of THIS scope
end
Of course you can simulate this with defer
local obj = my_complex_object()
defer getmetatable(obj).__close() end
but the api/protocol is up to you, while with <toclose> there is a
single mechanism that will be shared among all the lua libraries.
(I.e. with defer, someone would use a finalize method, others a
metamethod, others an additional return value, and so on).
However, I found a Go-like defer more elegant too (and probably more
"luaish"). So probably this is a issue of tradeoff... for now I am
slightly more incline to <toclose>.