lua-users home
lua-l archive

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


> > In Python you can pass the function object as a default parameter to
> > work around this problem. However, I have no idea how to work around
> > this in Lua. Upvalues don't work, because the variable is created
> > after the function definition has been evaluated. Just for the fun of
> > it, ideas anyone?
> 
> As you said, it's not straightforward, but you can do it with upvalues:
> 
> function f()
>   local g = {}
>   g.f = function()
>     %g.f()
>   end
> end
	Weird isn't it?  I have never used anonymous functions in other
languages to make a comparison.  Maybe there are a known solution.  How
does it work in Lambda-calculus?
	It also can be done with the debug API (BTW, I don't think it's
a better solution):

function anonymous(n)
   if n < 0 then return end
   print(n)
   getstack(1,'f').func (n-1)
end

	It does work in Lua 4.0 alpha!
		Tomas