lua-users home
lua-l archive

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


It was thus said that the Great bil til once stated:
> John Belmonte <john@neggie.net>:
> >
> > I noticed a possible issue of the __close metamethod:  nil is a valid value for raising errors, but __close cannot distinguish between nil error and no error.
> 
> Would you mind to show here 2 or 3 small and typical Lua "Code
> snippets", where I could recognize, when __close is invoked "normally"
> or "by error event"?
> 
> ... this different invocation somehow sounds interesting to me, but I
> do not really feel sure what this means "in real Lua life".

  Here's a snippit that shows both a "normal" close and an "error" close.  

local function closetest()
  return setmetatable({},{__close = function(...) print("CLOSE",...) end })
end

local function goodclose()
  local foo <close> = closetest()
end

local function badclose()
  local foo <close> = closetest()
  foobar()
end

goodclose()
badclose()

When I run this, I get:

CLOSE	table: 0x8319e08	nil
CLOSE	table: 0x8319518	x.lua:12: attempt to call a nil value
(global 'foobar')
stack traceback:
	x.lua:12: in local 'badclose'
	x.lua:19: in main chunk
	[C]: in ?
lua: x.lua:12: attempt to call a nil value (global 'foobar')
stack traceback:
	x.lua:12: in local 'badclose'
	x.lua:19: in main chunk
	[C]: in ?

  -spc