lua-users home
lua-l archive

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


On 19 July 2018 at 21:22, Soni "They/Them" L. <fakedme@gmail.com> wrote:
> Oh, I just realized having a "return" or "break" inside the block/for loop
> would cause issues... Hmm... Oh well, at least I tried I guess :/

Yes, that's why people call for an actual feature to support this. It
is not syntactic sugar: implicit immediate release of resources cannot
be implemented in Lua as is.

Note *implicit immediate* together:

You can have implicit release of resources with __gc, but that's not
immediate. (And not being immediate is a problem: I have had
real-world bugs caused by that; files provided by the standard io
library do close() on __gc, but if you rely on that in a loop, your
code works fine for years until you run a loop on a directory with
thousands of files, and then you get a "too many open files" failure.)

You can have immediate release of resources by calling foo:close() by
hand in every possible exit of your function, but that's not implicit.
(And not being implicit is a problem: throw the first stone they who
never forgot to close()/free()/release() a resource is some of their
code paths — that's why languages have garbage collection for memory
management, after all — but memory is not the only kind of resource
managed in a program.)

-- Hisham