lua-users home
lua-l archive

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


Hi. I just discovered this behavior by accident. I'm using 5.02. The
following code is supposed to modify obj so that its methods can be
called with obj.foo(...) instead of obj:foo(...).

1: obj = Obj:new()
2: for name, fn in pairs(Obj) do
3:    if type(fn)=="function" then
4:       obj[name] = function(...) return fn(obj, unpack(arg)) end
5:    end
6: end

When I call obj.foo(...), I get an error saying I tried to call 'fn',
a nil value. If I change things to:
...
3:   if type(fn)=="function" then
4:      local func = fn
5:      obj[name] = function(...) return func(obj, unpack(arg)) end
...

Then everything works fine. It's as if the loop variable 'fn' is not
registered as an upvalue in the closure created. Is this a buglet, or
am I not understanding something correctly?

-Paul