Order of Function Declarations

I am making excellent progress in learning/developing in LUA thanks to all of the examples here..awesome.. but suddenly I got stuck on what I think is probably a simple solution, but an hour later, I am still stuck.

Here is a simplified idea of my problem:

local function A()
-- calls function b
B()
end
local function B()
--calls function a
A()
end

my problem is that above doesn't work... in function A(), I can't call function B() unless I define function B ABOVE or PRIOR to it being called, but then if I do that and declare B above A, then B can't call A ...circular...

I have never encountered this in "higher" languages, I'm sure there must be some work-around Lua ... any ideas?? Help !

THanks,

There are many solutions to this, the simplest for now is just to use forward declarations.

1
2
3
4
5
6
7
8
9
10
11
12
13
local a = nil
local b = nil
 
a = function()
     print( "In A" )
end
 
b = function()
     print( "In B" )
     a()
end
 
b()

as simple as this :)

1
2
3
4
5
6
7
8
9
10
11
local A,B
 
function A()
  -- calls function b
  B()
end
 
function B()
  --calls function a
  A()
end

Renjith, that can get into a deadlock situation as A -> B and B-> A, which will again spawn B and so on.

cheers,

?:)

SING: never ending story ... maybe til the stack explodes

but the problem itself should be resolved.

views:1642 update:2011/10/17 21:25:02
corona forums © 2003-2011