lua-users home
lua-l archive

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


There are two main scenarios I can imagine:
1)

local src=io.open(fn_src,"rb") or error "details"
do<defer> io.close(src) end
local dst=io.open(fn_dst,"wb") or error "details"
do<defer> io.close(dst) end
...

I think this it not right way it should look like.
2)

local scope=make_scope "desctiption"  do <defer> scope.cleanup() end
local src=scope.io.open(fn_src,"rb")
local dst=scope.io.open(fn_dst,"rb")
...

But it can be expressed without defer
2a)

local scope <close> = make_scope "desctiption"
local src=scope.io.open(fn_src,"rb")
local dst=scope.io.open(fn_dst,"rb")

But do<localonly> can be very handy for misspelling detection.

сб, 12 окт. 2019 г. в 16:56, pocomane <pocomane_7a@pocomane.com>:
>
> I added a new annotation to the my previous patch to the "do" block:
> <defer>. A block like:
>
> ```
> do <defer>
>   -- ...
> end
> ```
>
> is equivalent to:
>
> ```
> local _ <close> = defer(function()
>   -- ...
> end)
> ```
>
> except for the variable name. "defer" is a new global function equivalent to:
>
> ```
> function defer(func)
>   return setmetatable({},{__close = func })
> end
> ```
> You can redefine it to change the behaviour of `do<defer>end`.
>
> The result is that the content of the `do<defer>end` block will be run
> at end of the scope.
>
> I attached the whole patch that enables the other attributes too (they
> are all listed in the README).
>
> pocomane
>
> PS: In the C code, I create an auxiliary <close> variable for each
> defer block. Probably there are simpler methods, but i do not know the
> lua internals that much.