Local Function Forward Declarations

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

Local Function Forward Declarations

Mark Gabby
Currently, it's possible to forward-declare a local function in Lua like so:

local forward_declared_function

local function calling_function()
    print "calling function"
    forward_declared_function()
end

forward_declared_function = function()
    print "forward declared function"
end

This looks kinda ugly, though. I'd like it better if Lua supported this:

local forward_declared_function

local function calling_function()
    print "calling function"
    forward_declared_function()
end

local function forward_declared_function()
    print "forward declared function"
end

With Lua 5.2.1, if you call calling_function() after defining the first, it prints:
calling function
forward declared function

but if you call it after the second, it prints, then fails:

calling function
lua: debug.lua:26: attempt to call upvalue 'forward_declared_function' (a nil value)
stack traceback:
    debug.lua:26: in function 'calling_function'
    debug.lua:33: in main chunk
    [C]: in ?

Why does the first work, but not the second?
Reply | Threaded
Open this post in threaded view
|

Re: Local Function Forward Declarations

Rena

On 2012-09-29 3:43 PM, "Mark Gabby" <[hidden email]> wrote:
>
> Currently, it's possible to forward-declare a local function in Lua like so:
>
> local forward_declared_function
>
> local function calling_function()
>     print "calling function"
>     forward_declared_function()
> end
>
> forward_declared_function = function()
>     print "forward declared function"
> end
>
> This looks kinda ugly, though. I'd like it better if Lua supported this:
>
> local forward_declared_function
>
> local function calling_function()
>     print "calling function"
>     forward_declared_function()
> end
>
> local function forward_declared_function()
>     print "forward declared function"
> end
>
> With Lua 5.2.1, if you call calling_function() after defining the first, it prints:
> calling function
> forward declared function
>
> but if you call it after the second, it prints, then fails:
>
> calling function
> lua: debug.lua:26: attempt to call upvalue 'forward_declared_function' (a nil value)
> stack traceback:
>     debug.lua:26: in function 'calling_function'
>     debug.lua:33: in main chunk
>     [C]: in ?
>
> Why does the first work, but not the second?

I believe it's because the "local function" statement declares a new local variable, shadowing the old one. Code before it is still referring to the old one which never gets set.

Reply | Threaded
Open this post in threaded view
|

Re: Local Function Forward Declarations

Rob Hoelz-2
On Sat, 29 Sep 2012 15:52:00 -0400
Rena <[hidden email]> wrote:

> On 2012-09-29 3:43 PM, "Mark Gabby" <[hidden email]> wrote:
> >
> > Currently, it's possible to forward-declare a local function in Lua
> > like
> so:
> >
> > local forward_declared_function
> >
> > local function calling_function()
> >     print "calling function"
> >     forward_declared_function()
> > end
> >
> > forward_declared_function = function()
> >     print "forward declared function"
> > end
> >
> > This looks kinda ugly, though. I'd like it better if Lua supported
> > this:
> >
> > local forward_declared_function
> >
> > local function calling_function()
> >     print "calling function"
> >     forward_declared_function()
> > end
> >
> > local function forward_declared_function()
> >     print "forward declared function"
> > end
> >
> > With Lua 5.2.1, if you call calling_function() after defining the
> > first,
> it prints:
> > calling function
> > forward declared function
> >
> > but if you call it after the second, it prints, then fails:
> >
> > calling function
> > lua: debug.lua:26: attempt to call upvalue
> > 'forward_declared_function' (a
> nil value)
> > stack traceback:
> >     debug.lua:26: in function 'calling_function'
> >     debug.lua:33: in main chunk
> >     [C]: in ?
> >
> > Why does the first work, but not the second?
>
> I believe it's because the "local function" statement declares a new
> local variable, shadowing the old one. Code before it is still
> referring to the old one which never gets set.
That's correct.  What Mark is asking for is possible like this:

local forward_declared_function

local function calling_function()
  print 'calling function'
  forward_declared_function()
end

function forward_declared_function()
  print 'forward declared function'
end

That may be a little unclear, though.

-Rob

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Local Function Forward Declarations

Michal Kolodziejczyk-3
In reply to this post by Mark Gabby
On 29.09.2012 21:42, Mark Gabby wrote:

> Why does the first work, but not the second?

Functions are first class values, so your example is similar to:

local a
a=1
print(a)
local a
print(a)

So you basically declare another variable (with the same name "a", which
overrides the previously declared one) within the same scope (closure)
with NULL.

I would suggest you rewrite your example as:

local forward_declared_function, calling_function

function calling_function()
  print "calling function"
  forward_declared_function()
end

function forward_declared_function()
  print "forward declared function"
end

That looks less ugly (to me at least).

Regards,
miko

Reply | Threaded
Open this post in threaded view
|

Re: Local Function Forward Declarations

Mark Gabby
OK, I understand now. Thanks for the plentiful assistance from many directions.

I understand why it works this way now and why my second example is saying something valid, but different.

From a readability standpoint, I still don't really like how the "local" tag has to be omitted if you've already declared the variable for a local function, but that's the way the language works.