lua-users home
lua-l archive

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


Peter Hill wrote:
> itten in lua 5 ...
> 
> My mistake. I remeber reading something about them no longer being used but
> that may have been something on the mailing list a while ago.
> 
> They have changed nature, though. I just tried:
>     local x=123
>     f = function() return %x end
>     x=456
>     print(f())
> 
> Lua4.0 returns 123 as expected, while Lua5.0 doesn't even get past the
> definition of "f". It says "global upvalues are obsolete". I need to look
> more closely at the new upvalues.

AFAICS, the meraning of the word "upvalue" has completely 
changed. Now, it simply refers to the "hidden parameters" 
you can give a C function when you register it for use with Lua.

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.

To do what you want to do, I think you'll need to make 
a little "detour".


function makef(val) 
   return (function() return val; end); 
end

function fie()                                                   
     local x=123
     f = makef(x) -- here x is passed by value and dereferenced
immediately                             
     x=456
     print(f())
end

fie() will now print 123.

-- 
"No one knows true heroes, for they speak not of their greatness." -- 
Daniel Remar.
Björn De Meyer 
bjorn.demeyer@pandora.be