David Manura

lua-users home
wiki

Showing revision 195

Wiki pages

There are Lua wiki pages and projects I've been involved in.

Code and Projects:

General Topics, Patterns, Tricks, and Design:

Comments/Annotations on Other Documents:

Lua 5.2 Wishlist

These changes I want are included in the 5.2 work releases (LuaFiveTwo):

Here's other ideas (not currently addressed in 5.2 work releases):

This is a biggie. JohnBelmonte has strongly advocated it, and various people have provided patches with various implementations (above link). There's ways to do it in 5.1 with wrapper functions utilizing pcall's and anonymous functions everywhere, but it's not very nice.

Proper error handling in the presence of resource allocation in Lua can be clumsy without this. One of the simpler solutions is a "defer" statement like in the Google Go language (or D language "scope(exit)"). Compare:

-- Lua 5.1, example without exceptions
local function readfile(filename)
  local fh, err = io.open(filename)
  if not fh then return false, err end
  local data, err = fh:read'*a'
  -- note: in this case, the two fh:close()'s may be moved here, but in general that is not possible
  if not data then fh:close(); return false, err end
  fh:close()
  return data
end
-- Lua 5.1, example with exceptions, under suitable definitions of given functions.
local function readfile(filename)
  return scoped(function(onexit)  -- based on pcall
    local fh = assert(io.open(filename)); onexit(function() fh:close() end)
    return assert(fh:read'*a')
  end)
end

-- proposal, example without exceptions
local function readfile(filename)
  local fh, err = io.open(filename); if not fh then return false, err end
  defer fh:close()
  local data, err = fh:read'*a'; if not data then return false, err end
  return data
end
  -- note: "local val, err = io.open(filename); if not val then return false, err end" is a common
  -- pattern and perhaps warrants a syntax like "local val = returnunless io.open(filename)".
-- proposal, example with exceptions
local function readfile(filename)
  local fh = assert(io.open(filename)); defer fh:close()
  return assert(fh:read'*a')
end
-- proposal, example catching exceptions
do
  defer if class(err) == 'FileError' then
    print(err)
    err:suppress()
  end
  print(readfile("test.txt"))
end

-- alternate proposal - cleanup code by metamechanism
local function readfile(filename)
  scoped fh = assert(io.open(filename)) -- note: fh:close() or getmetatable(fh).__close(fh) called on scope exit
  return assert(fh:read'*a')
end

function foo() error("fail", 2) end
function bar() foo() end -- any way for bar not to increase the error level when invoking foo?
foo()  -- error level ok
bar()  -- error level wrong

local two
local function one() two() end
function two() one() end -- forward declared local (misleadingly looks like a global declaration)

Some other things, not necessary related to Lua core development:


RecentChanges · preferences
edit · history · current revision
Edited February 23, 2012 3:46 am GMT (diff)