Hi,
I don’t understand why this code isn’t working, please help
local function\_name1 local function\_name2 function\_name1() function\_name2() local function function\_name1() print (10) end local function function\_name2() print (20) end
Hi,
I don’t understand why this code isn’t working, please help
local function\_name1 local function\_name2 function\_name1() function\_name2() local function function\_name1() print (10) end local function function\_name2() print (20) end
** CORRECTED SOME TYPOS AFTER ORIGINAL POST**
You’re doing that wrong.
This is the right way
-- Declare two variables (currently with nil in them) -- They are now in scope from here to the end of the file local function\_name1 local function\_name2 -- Assign functions to the variables -- i.e. DEFINE the functions and assign them to their respective -- forward declared variables. function\_name1 = function() print("In function\_name1()") -- Forward declaration allows function\_name1() to call function\_name2() -- even though it is DEFINED after we DEFINE function\_name1(). -- -- How? -- -- Because we forward declared function\_name1 and function\_name2, -- we can now refer to them anywhere in the file. -- If we do so in the body of a function, that function will do a look-up -- to see if the variable has been defined yet. -- -- By the time you call function\_name1(), function\_name2() will have been defined. -- function\_name2() end function\_name2 = function() print("In function\_name2()") end -- -- You cannot call them till AFTER they have been defined. -- function\_name1() function\_name2()
Running the above code will print this to the console:
In function\_name1() In function\_name2() In function\_name2()
Forward declaration is about using the scoping rules of Lua to your advantage.
What you were doing in the original code was trying to call a (as yet) undefined function. The variable was in scope, but still nil.
Also, you didn’t actually use those variables later. Instead you created two new local variables with the same names.
See my code versus yours to get clarity on the difference.
Ok, Thank you for the reply.
is this is OK?
local test1 local test2 local function main() test1() test2() end test1 = function () print("test1") end test2 = function () print("test2") end main()
That should work.
While that should work, unless you have a reason to need to forward declare a function, I would recommend writing the functions before you call them. It’s not always possible, but most of the time you can write your functions first.
Lua is a one-pass system, unlike languages like C that are two-pass, so if you’re forward declaring because that’s the C way, stylistically the Lua way would be to move your main to the end (if you even need main()).
Rob
Thank you guys for the help!
this is a real limitation in Lua (one pass v two pass). for any larger project where function A is called from B and also C yet B can be called from C, this is just not feasible.
one solution I do is to globally declare all my “helper functions” and then localise them when needed. this has all the benefits of globally scoping functions but no hit for being global.
much like one would do
local random = math.random
Thanks
** CORRECTED SOME TYPOS AFTER ORIGINAL POST**
You’re doing that wrong.
This is the right way
-- Declare two variables (currently with nil in them) -- They are now in scope from here to the end of the file local function\_name1 local function\_name2 -- Assign functions to the variables -- i.e. DEFINE the functions and assign them to their respective -- forward declared variables. function\_name1 = function() print("In function\_name1()") -- Forward declaration allows function\_name1() to call function\_name2() -- even though it is DEFINED after we DEFINE function\_name1(). -- -- How? -- -- Because we forward declared function\_name1 and function\_name2, -- we can now refer to them anywhere in the file. -- If we do so in the body of a function, that function will do a look-up -- to see if the variable has been defined yet. -- -- By the time you call function\_name1(), function\_name2() will have been defined. -- function\_name2() end function\_name2 = function() print("In function\_name2()") end -- -- You cannot call them till AFTER they have been defined. -- function\_name1() function\_name2()
Running the above code will print this to the console:
In function\_name1() In function\_name2() In function\_name2()
Forward declaration is about using the scoping rules of Lua to your advantage.
What you were doing in the original code was trying to call a (as yet) undefined function. The variable was in scope, but still nil.
Also, you didn’t actually use those variables later. Instead you created two new local variables with the same names.
See my code versus yours to get clarity on the difference.
Ok, Thank you for the reply.
is this is OK?
local test1 local test2 local function main() test1() test2() end test1 = function () print("test1") end test2 = function () print("test2") end main()
That should work.
While that should work, unless you have a reason to need to forward declare a function, I would recommend writing the functions before you call them. It’s not always possible, but most of the time you can write your functions first.
Lua is a one-pass system, unlike languages like C that are two-pass, so if you’re forward declaring because that’s the C way, stylistically the Lua way would be to move your main to the end (if you even need main()).
Rob
Thank you guys for the help!
this is a real limitation in Lua (one pass v two pass). for any larger project where function A is called from B and also C yet B can be called from C, this is just not feasible.
one solution I do is to globally declare all my “helper functions” and then localise them when needed. this has all the benefits of globally scoping functions but no hit for being global.
much like one would do
local random = math.random
Thanks