lua-users home
lua-l archive

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




Il mar 11 giu 2019, 20:14 Doug Currie <doug.currie@gmail.com> ha scritto:
On Tue, Jun 11, 2019 at 11:15 AM Daurnimator <quae@daurnimator.com> wrote: 
local contents
do
    local f = io.open("myfile")
    defer f:close() end
    contents = f:read()
end

I was going to post something like this multiple times in the past months. I thought that I was going to use <toclose> to build a wrapper for a Go-like defer in the 99% of my code.

But then I realized that I was missing the point. The <toclose> mechanism let you to do actual RAII returning objects that are able to close themself, you only have to decide when. E.g.:

local <toclose> x = my_complex_object_factory()

VS

local x = my_complex_object_factory()
...
return x

Right now, my only complain with the <toclose> is the term "close" that I found a bit vague and over-loaded (conceptually).

Well... And the error handling things that arised in other threads of this mailing list.


If we're adding keywords...

local contents
local f = io.open("myfile")
do
    contents = f:read()
finally
    f:close()
end

Imho this miss the point even more since the closing stuff is very distant from the initialization. What is the difference with just calling "close" at end of the scope?