lua-users home
lua-l archive

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


I recently hit on an interesting ponderance.

I often use the construct:

 local foo = function(bar) ........ end

to localise functions to where they're used (particularly when
using them purely in the context of the function in which they
are called).

However, I recently came across a need to make one of these
functions recursive.

Unfortunately using %foo() doesn't work because the 'foo' variable hasn't
been assigned a value, so %foo is 'nil' (understandably so).

I then thought - aaah - use a table!

local t = { foo = function(bar) ...... end }

But Lo, %t will be nil also, so %t.foo won't work :(

So, I am currently stuck with:
  local t = {}
  t.foo = function(bar) ....... end

so that I can call %t.foo() to recurse.

What I'd love to see, is the ability to get a ref to the 'current'
function, perhaps as an implicit variable, a bit like 'this' in java and
C++.

So that I could recurse by calling:
	currentfunc()
rather than messing about with tables etc.

I'm happy to hack about and try to add something myself, but I feel
that it would be nice to have this in the lua spec itself.

Any thoughts?

Regards,

Daniel