lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




2015-08-26 9:53 GMT+08:00 Nan Xiao <xiaonan830818@gmail.com>:
Hi all,

I am a little confused about the following code in Pil:

    i = 32
    local i = 0
    f = load("i = i + 1; print(i)")
    g = function () i = i + 1; print(i) end
    f() --> 33
    g() --> 1

Why does function g() always get local i, not global i? If function
g() wants to
get the global i, how does it do?
 
g is a closure defined in parent scope where local i is an upvalue.
_G.i can be used to access the global i.