lua-users home
lua-l archive

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


Hi.
There is pcall() ('try') without 'finally' to properly free resources in the called functions.
What do you think about adding the optionally finally block statement to functions?
This finally block must be executed once whenever returned from function or error throwed.

funcbody ::= '(' [parlist1] ')' block1 [finally block2] end

Examples:

---- Parsing error: finally block cannot return

function a()
  ...
finally
  return
end

---- Close file

function g()
  error("Exception")
end

function f(filename)
  local file = io.open(filename)
  g()
finally
  if file then file:close() end
end

---- Finalization chain: prints a b c

function c() print'c' end

function b() return c() finally print'b' end

function a() return b() finally print'a' end