lua-users home
lua-l archive

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


Now I understand that the % is just for "compatibility", but now I would say that it is a bit dangerous because not actually truely compatible. Peter Hill is right in saying that the behaviour has changed (altough I don't get the error he is reporting unless I forget to make x local) and the result with lua 5.0beta is indeed 456, and 123 with lua 4.0.

Furthermore, it is not mentioned in the manual (of course it is beta, so this may explain that)

One way to get original lua 4 behaviour was

local x=123

do
  local x = x
  f = function() return %x end
end

x = 456

print (f())

Now this gives 123 with both lua 4 and 5 ...

Eero Pajarre wrote:

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