lua-users home
lua-l archive

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


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