[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Yet another proposal for <toclose> (was Re: [ANN] Lua 5.4.0 (alpha) now available)
- From: pocomane <pocomane_7a@...>
- Date: Fri, 28 Jun 2019 21:46:49 +0200
On Fri, Jun 28, 2019 at 6:12 PM Sergey Kovalev <kovserg33@gmail.com> wrote:
>
> Can you give an example?
>
> > What about coroutines? I.e. "body" resumes a coroutine that calls "auto" (not protected by an inner "scope").
```
local function my_handler(f)
print("closing",f)
end
local a_coroutine = coroutine.create(function()
local auto = coroutine.yield()
local f1 = auto(my_handler){ "file 1" }
coroutine.yield()
local f2 = auto(my_handler){ "file 2" }
coroutine.yield()
print("using", f1, "and", f2)
end)
coroutine.resume(a_coroutine)
scope(function(auto)
coroutine.resume(a_coroutine, auto)
coroutine.resume(a_coroutine)
end)
coroutine.resume(a_coroutine)
```
Yes, I know, I should use the scope/auto INSIDE the coroutine. Or I
have to put the last resume INSIDE scope. But the point is that with
this implementation I can also mess around like in the example. With a
real "Scoped annotation" like <toclose> I cannot.
In the past I have used something like your scope/auto, but exposing
auto outside the scope [1]. I admit that passing auto to the body
somehow protect you from some mistake, but I think it is not enough.
pocomane
[1] It was something like:
```
local list
local function defer( func )
if not list then error("defer autside a scope", 2) end
list[1+#list] = func
end
local function scope( block )
local old_list = list
list = {}
local ok, err = pcall(block)
for i = 1, #list do
local ok, err = pcall(list[i])
if not ok then warn(err) end
end
if not ok then error(err, 1) end
list = old_list
end
----------------------------
-- usage example:
scope(function()
local f1 = "file 1"
defer(function() print("closing", f1) end)
local f2 = "file 2"
defer(function() print("closing", f2) end)
print("using",f1,"and",f2)
end)
```