Re: Lua5.0 new "For" question
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Lua5.0 new "For" question
 
- From: Eero Pajarre <epajarre@...>
 
- Date: 2002年12月22日 22:05:59 +0200
 
Björn De Meyer wrote:
you can still say
function fie()
 local x=123
 f = function() return %x end
 x=456
 print(f())
end
But that one seems to behave exactly the same as when not
using the %, namely it will print 456. Because of the 
lexical scoping the x in the anonymous function is the 
local x of fie(). The value of x is only "taken" 
when f() is called.
Looks like % is just accepted for compatibility,
but otherwise ignored (except the global upvalue message)
So your example creates a closure with or without the "%".
See:
function fie()
 local x=789
 f = function() return x end --global f
 x=123
end
fie() -- call function which creates function f
print(f()) -- prints 123
x=456
print(f()) -- still prints 123
		Eero