Recursive local functions

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Recursive local functions

Daniel Silverstone
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

Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

Luiz Henrique de Figueiredo
>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++.

This "need" comes up every once in a while.
But is this really needed?
I agree that a language solution would be nice, but I can't see one nor can I
see a real need for this. How common are such recursive functions?
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

Erik Hougaard
----- Original Message -----
> But is this really needed?
> I agree that a language solution would be nice, but I can't see one nor
can I
> see a real need for this. How common are such recursive functions?

I agree, this will not make Lua a better system - only bigger - lets focus
on keeping it small :-)

/Erik

BTW: I suggest everybody to read Roberto book (The draft) - This is good
reading - even for Lua oldtimers !


Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

Luiz Henrique de Figueiredo
In reply to this post by Daniel Silverstone
>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.

Come to think of it, this already exists!
Write "getinfo(1).func" inline or define

 function currentfunc()
  return getinfo(2).func
 end

Yeah, metamechanisms instead of language features!
(Even I sometimes get suprised by how neat Lua is, if I say so myself...)
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

Reuben Thomas-3
> Come to think of it, this already exists!
> Write "getinfo(1).func" inline or define
>
>  function currentfunc()
>   return getinfo(2).func
>  end
>
> Yeah, metamechanisms instead of language features!

This is all very well, but section 7.4 of the manual says "Please resist the
temptation to use [the debugging interface] as a usual programming tool".
(BTW, "a usual programming tool" is not comfortable English; how about
"...the temptation to use them for normal programming"?

So it's all very well if this function is permitted for normal use, but at
the moment it's not. This would seem to be a case in which having a function
"recurse()", which could be implemented as "getinfo(1).func", would be
useful, to hide the implementation details.

While I look at it, I can't see a "func" field mentioned anywhere in the
docs...does this have something to do with supplying "f" as "what"? In that
case, it would seem more sensible to call getinfo(1, "f")...

This is another area of the documentation that could do with tidying up a
little for 4.1.

-- 
http://sc3d.org/rrt/ | Si hoc legere scis nimium eruditionis habes.


rje
Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

rje
In reply to this post by Luiz Henrique de Figueiredo
On Thu, Jan 04, 2001 at 02:06:27PM -0200, Luiz Henrique de Figueiredo wrote:
> >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++.
> 
> This "need" comes up every once in a while.
> But is this really needed?
> I agree that a language solution would be nice, but I can't see one nor can I
> see a real need for this. How common are such recursive functions?

Often, if you write the evil code I do ;-)

Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

Daniel Silverstone
In reply to this post by Reuben Thomas-3
On Thu, Jan 04, 2001 at 05:09:21PM +0000, Reuben Thomas wrote:
> So it's all very well if this function is permitted for normal use, but at
> the moment it's not. This would seem to be a case in which having a function
> "recurse()", which could be implemented as "getinfo(1).func", would be
> useful, to hide the implementation details.

Indeed - however, it is /impossible/ (AFAICT) to implement such a
function, even in 'C' without taking in the ldblib library or #include'ing
"luadebug.h" which is /NOT/ what I want to have to do.

It seems to me, that there is room, within the language specification, for
a set (or perhaps /one/) implicit variable in the 'var' section, or
perhaps in the 'varorfunc' bit, allowing for 'self' or 'this' or 'cfunc'
or something.

Whilst I agree wholeheartedly with keeping the language as small as
possible, I do feel that there is a good argument for not pushing language
features into meta syntax, when said metasyntax relies on /DEBUGGING/
aids.

I will look at the bits of the lua source pertaining to lua_getstack() and
decide whether a minor reimplementation to cope with getting the function
directly 'up' is sensible, or whether it ought to be a language thing
(IMO).

I'm still reasonably sure that this would be a /minor/ language addition
/well worth it/ in the long run.

Regards,

Daniel


Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

Reuben Thomas-3
In reply to this post by rje
> > I agree that a language solution would be nice, but I can't see one nor can I
> > see a real need for this. How common are such recursive functions?
>
> Often, if you write the evil code I do ;-)

I came across exactly this problem a few days ago, while writing a parser
for Scheme. I wanted to share some state (the string being parsed) between
two mutually recursive functions (one to parse an atom, one to parse a
list). This naturally led to wrapping up the two functions in an outer
function which was passed the string to parse.

Recursion is very common if you program in a functional style, and since
there's no let...in... construct in Lua, wrapping up functions in order to
use upvalues is a nice way to do implicit parameters. So I would imagine
this problem being quite common.

-- 
http://sc3d.org/rrt/ | wit, n.  educated insolence (Aristotle)


Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

Steve Dekorte-4
In reply to this post by Daniel Silverstone
If you're doing OO programming in lua, this isn't a problem:

local myObject = {}

function myObject:foo(bar)
  ... self:foo(bar2) ...
end

Steve

rje
Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

rje
On Thu, Jan 04, 2001 at 10:44:11AM -0800, Steve Dekorte wrote:
> 
> If you're doing OO programming in lua, this isn't a problem:
> 
> local myObject = {}
> 
> function myObject:foo(bar)
>   ... self:foo(bar2) ...
> end

Doing this is a bit of overkill, if the 'class' in question only has one
method, and no externally accessable variables.

Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

Steve Dekorte-4
In reply to this post by Daniel Silverstone
On Thursday, January 4, 2001, at 10:49 AM, [hidden email] wrote:

> On Thu, Jan 04, 2001 at 10:44:11AM -0800, Steve Dekorte wrote: 
> >  
> > If you're doing OO programming in lua, this isn't a problem: 
> >  
> > local myObject = {} 
> >  
> > function myObject:foo(bar) 
> >   ... self:foo(bar2) ... 
> > end 
>  
> Doing this is a bit of overkill, if the 'class' in question only has one 
> method, and no externally accessable variables. 

I agree, but chances are that whatever your function is manipulating has
other functions that manipulate it which can be collected into a proper object.

Steve

rje
Reply | Threaded
Open this post in threaded view
|

Re: Recursive local functions

rje
On Thu, Jan 04, 2001 at 11:10:30AM -0800, Steve Dekorte wrote:
> On Thursday, January 4, 2001, at 10:49 AM, [hidden email] wrote:
> > On Thu, Jan 04, 2001 at 10:44:11AM -0800, Steve Dekorte wrote: 

<snip>

> > > function myObject:foo(bar) 
> > >   ... self:foo(bar2) ... 
> > > end 
> >  
> > Doing this is a bit of overkill, if the 'class' in question only has one 
> > method, and no externally accessable variables. 
> 
> I agree, but chances are that whatever your function is manipulating has
> other functions that manipulate it which can be collected into a proper object.

Yes; possibly.  I suppose the only argument I can think of agaist that at
the moment is that it doesn't suit my programming style :)