local MyFunction = function(a) return 999 end
local MyFunction = function(a) if a < 100 then return MyFunction(a*2) else return a end end
print(MyFunction(1))
Can you guess the output? The answer is 999.
local MyFunction = function(a) return 999 end
MyFunction = function(a) if a < 100 then return MyFunction(a*2) else return a end end
print(MyFunction(1))
What about this? All I did was remove "local" from the redefinition of MyFunction() and since the function has already been declared as local, that shouldn't matter, right? Well, it does, because now the output is 128.
What's going on?
Comments
@zvardin, it is perfectly legal to redefine, either variables or functions in Lua and it is quite often rather useful.
best regards
local x = function() end
Sometime it doesn't give a good result for me also (function not being called, and other bad things)
instead i did something like this
local function MyFunction(a) return 999 end
local function MyFunction(a) if a < 100 then return MyFunction(a*2) else return a end end
local foo=2
:
-- lots code
:
local foo=1
you would not notice the error.
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
or something along those line
http://www.lua.org/pil/6.2.html
I think when the compiler sees a global variable name that is not yet defined, it creates an empty named slot for it immediately. Hence in the recursive call when the second function is global it already knows that MyFunction exists and just calls through it. Since globals are really just fields in a table this probably applies to functions stored in all table fields too.
Local variables, being held on the stack are probably just artefacts of the compiler, which is which they are handled differently. As the above article suggests, you can't have a recursive call through a local without first forward referencing it. Recursive calls through globals are okay without the forward reference though.
best regards
local a = 1;
a = 2; -- Still the same, local variable. NOT a new, global variable.
In which case I don't know the answer. However, the two examples are not equivalent. In the first you are defining two variables it is just that the scope of the second overrides the first. In the second case you have one local that gets reassigned.
best regards
However, I can think of several situations where redefining table or global variables is useful. Typically that is the way you can hook into existing behaviour and extend it. The GiderosCodingEasy library by @ar2rsawseen does it a lot.
best regards
Here's a stab at things... It's all about chunks and closures, the way Lua handles things.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
In the second case ("local foo = function() end; foo = function() ... end") the same (local) variable gets a new value and the function call references the same variable,
so it all works. By the time when the function is called, there is only one variable that has the function value (so it becomes a recursive function).
I've answered a somewhat similar SO question last month (http://stackoverflow.com/a/15867293/1442917) that covers another syntax that has a subtle difference: "local function foo() end" is NOT the same as "local foo = function() end" because the first one allows "foo" to be referenced from the body of the function (new name effectively becomes "visible" to the function), while in the second case if "foo" is referenced, it will use whatever value was assigned to "foo" previously (local, upvalue, or global).
Paul.