This site works best with JavaScript enabled. Please enable JavaScript to get the best experience from this site.
Quote from sylvanaarThe do block is the equivalent of the following C code: void myFunc() { static int a = 1; ... do stuff }
void myFunc() { static int a = 1; ... do stuff }
{ stuff in an isolated scope }
Quote from Valana_TBWouldn't it rather be more simply: { stuff in an isolated scope }? A do block isn't defining a function, unless I missed something.
function makef() local a = 1 return function () a=a+1 return a end end f = makef()
Quote from XinhuanResults aren't surprising. local (stack access) > upvalue (heap access) > global (_G lookup)
Wouldn't it rather be more simply:
? A do block isn't defining a function, unless I missed something.
It wasnt about the scope it was about the static variable.
The stereotypical example of using a do block is to simulate C static initialization
[php]do
-- Variable is initialized once
local a = 1
-- Then it is used in a function
function f()
a=a+1
return a
end
end[/php]For this you must use a do block, because the initialization needs to run during the execution of the main chunk.
You can leave the do block out, but then its not the same as the C code I showed. This still isnt quite the same - but its close.
Further reading
http://www.lua.org/pil/4.2.html
You can do the same thing if you create the function dynamically
Nope for sure they aren't.
Seeing is believing, and I was also testing how thins are affected by passing it as a parameter.