[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why do we need “toclose” (or those angle brackets) at all?
- From: David Heiko Kolf <david@...>
- Date: Fri, 14 Jun 2019 15:42:18 +0200
Sergey Zakharchenko wrote:
> Paul,
>
>> Why do we need the ugly, weird-looking and controversial <toclose> notation at all?
>
> Indeed, so many people don't like it, but fail to notice one thing.
> You don't have to use it!
>
> local function scope(object)
> return next,{[object]=1},nil,object
> end
>
> for file in scope(io.open("file")) do
> print("I can use file ",file)
> end
Oh, I missed those changes to the for loop as well. This example however
would ignore any errors, io.open would return nil as the first return
value and the loop never runs.
I tried extending your example, but my result doesn't look pretty in my
opinion:
local function scoperesult (t, c)
if not c then
return true, table.unpack(t, 1, t.n)
else
return nil
end
end
function scope (...)
local t = { ... }
t.n = select("#", ...)
return scoperesult, t, nil, (...)
end
for _, f, err in scope(io.open("test.dat") do
print(f, err)
end
It works, but it requires a new temporary table and an otherwise unused
loop control variable.
I still hope for some syntax like
local <toclose> f, err = io.open("test.dat")
Or whatever is used instead of "<toclose>", I don't care that much about
it, it doesn't look particularly ugly to me.
Best regards,
David