lua-users home
lua-l archive

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


On Sat, Jul 2, 2011 at 6:09 PM, Javier Guerra Giraldez
<javier@guerrag.com> wrote:
> very interesting.  i'd chose to have a single error class, that
> returns self on _every_ function except :err(), that returns nil,error
> and :assert() that fails

And it's pretty easy to do:

-- err.lua
local function errf(self)
  return nil,self.msg
end

local function passthru(self,...)
  return self
end

local err = {
  __index = function(self,k)
    if k == 'err' then
      return errf
    elseif k == 'assert' then error(self.msg,2)
    else
      return passthru
    end
  end
}

function err.new(msg)
  return setmetatable({msg=msg},err)
end

print(err.new("borked"):meth(2):another():err())
err.new("throws"):stuff(42,'hello'):assert()
-----------------
--->
nil     borked
lua: err.lua:25: throws
stack traceback:
        [C]: in function 'error'
       ....

You can get Mark's style easily by monkey-patching the io library.
(The fact that most people would react in horror to this idea is one
measure of the difference between the Lua and Ruby communities)

steve d.