lua-users home
lua-l archive

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




On Mon, Jul 15, 2019 at 2:31 PM Sergey Kovalev <kovserg33@gmail.com> wrote:
вт, 16 июл. 2019 г. в 00:23, Gé Weijers <ge@weijers.org>:
>
>
> Something like his (untested):
>
> local function close_values(t)
>   for _, val in pairs(t) do
>     local <toclose> tmp = val
>   end
> end
>
> local <toclose> array = setmetatable({}, {__close = close_values})
>
> When 'array' goes out of scope close_values will be called, which will call __close on all the values in the array.
>
 
1. I mean using syntax
if local f,err=io.open ...
and for f,err=io.open ...

I probably would not use this construct, it's way too limited, I basically agree with you on that. I just wanted to make the point that you can construct an array that keeps you from leaking resources, in stead of using recursion. It'll still be clumsy.



--
--