View Single Post
04-13-11, 08:43 AM   #10
Xinhuan
A Chromatic Dragonspawn
 
Xinhuan's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 174
Originally Posted by Foxlit View Post
The difference you describe does not exist. Try illustrating it with a code example.
Foxlit is correct, there is no difference in the way upvalues and globals are handled in both cases.

To answer the original poster, there are still differences between the 2 methods, but it only affects recursive functions. Look at these 2 examples:

#1
Code:
local f = function()
    f()
end
f()
#2
Code:
local function f()
    f()
end
f()
When you run f() on #1, you will get the error "attempt to call global 'f' (a nil value)" because the f referred to within the function doesn't exist yet. The function is compiled first, then assigned to local f - that is, the local f doesn't exist until after the assignment. So during the function's compilation, the f referred to inside the function is assumed to be on _G["f"] since there isn't any existing upvalue f.

When you run f() on #2, you will get the "stack overflow" with a stack trace of f() calling itself, because the syntactic sugar of #2 is actually just a shortcut to do this:

#2
Code:
local f
f = function()
    f()
end
f()
Here, the upvalue referenced in the function compiles correctly.
__________________
Author of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, GatherMate2, Routes and Cartographer_Routes

Last edited by Xinhuan : 04-13-11 at 08:46 AM.
  Reply With Quote