lua-users home
lua-l archive

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

Thanks; I guess the use case with coroutines would indeed be difficult to replicate in a simple way.

On Mon, Nov 22, 2021 at 3:00 PM Roberto Ierusalimschy <> wrote:
> Since Lua is a language that provides basic mechanisms instead of builtin
> high-level constructs, I am wondering:  why were to-be-closed variables
> given special language-level support?  I think it should be possible to
> implement the equivalent using closures:
> [...]
> I left out some details, but I'm assuming that something like the above
> could be made to work.  If not, what am I missing?  But either way, I am
> wondering what went into the decision to give to-be-closed variables
> language-level support instead of having users construct them from Lua's
> mechanisms?

Although "something like the above" could be made to work, it is
hard to be sure it really covers all corners. A basic idea for
to-be-closed variables was to give the user a mostly bulletproof way
to garantee that the resource will be released.

Another thing that had great weight in the decision was its
interaction with coroutines and also with for-loops. For coroutines,
in particular, it provides a way to finalize an interrupted coroutine
that is quite tricky to ensure in other ways.

-- Roberto