lua-users home
lua-l archive

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


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