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 won't work :(

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

so that I can call 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

So that I could recurse by calling:
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?