|
steve:
What I actually ment was that Moonscript might have a chance to add try-catch syntax, but looking at your link that's not going to happen either.
As for the Moonscript example, a table of three elements and three closures won't have a big impact on performance, unless you accululate like a billion a second, in which case you have far bigger problems.
Peter:
The alternative is to assert:
local result = assert(func)
This has the same significance as a exception, i.e.: something unexpected happened, do with it what you want.
Alexey:
As steve already pointed out, short function syntax will likely never be implemented, which I think is for the good, however unfortunate it may be.
This also illustrates how unlikely try-catch is to be implemented in Lua. After all
try
print(x.y)
catch (err)
print(err)
would just be
local ok, err = pcall(function()
print(x.y)
end)
if not ok then
print(err)
end
with the most notable difference being that try-catch doesn't require function()end tokens.
Viacheslav:
I am not sure if I get your point. try-catch nor pcall would prevent these connections from accumulating, unless it is placed INSIDE the code, in which case you should wonder: "Was throwing that exception really worth it?". Or, if you call a external library, you should wonder: "Is there anything I can do to prevent my program from misbehaving on a error?". If you can't, you should switch library, switch database, or accept that you made the wrong choices or have to deal with a braindead API and just create a pcall.
Or, alternatively, you could do something like this:
local ok, err = pcall(function()
local f = db.open('file.txt')
library.call(f)
f:close()
end)
if not ok then
collectgarbage() -- force garbage collection, closing open database connections
error(err)
end
which is what a sensible database API would want you to do.