lua-users home
lua-l archive

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



On 31-Aug-04, at 7:32 PM, Nick Trout wrote:

You cannot declare functions in Lua. Local values will be concreted in
the function closure made. If you can't move func2 before func1 you'll
have to make a global variable that accesses func2. Global variables are
lazy evaluated so you can set them anytime before you need to use them.
Local variables are evaluated when they are parsed and the reference
made concrete.

Not quite; Asko is quite right:

local func1
   -- declares the local func1

local function func2(x) return func1(x + 1) end
  -- uses the local func1

function func1(x) return func2(x - 1) end
  -- assigns a value to the local func1


There was a discussion about this a while ago, iirc, but no-one came
up with a useful suggestion.

By the way, the same situation can occur with tables:

local tableOfFunctions

local function foo(x) return tableOfFunctions[x](42) end

tableOfFunctions.foo = foo