[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A question about function accessing local and global variables
- From: Nan Xiao <xiaonan830818@...>
- Date: Wed, 26 Aug 2015 11:52:24 +0800
Hi Jonathan, Luiz, Zejian,
I see! Thanks very much for all your comments!
Best Regards
Nan Xiao
On Wed, Aug 26, 2015 at 10:36 AM, zejian ju <juzejian@gmail.com> wrote:
>
>
> 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.
>