lua-users home
lua-l archive

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


Eero Pajarre wrote:
> 
> 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

Hmmm... so, If I understand correctly, the closure 
is "frozen" or "closed" once the local x is out of scope.
I find this one also interesting:


function fie()                                 
      local x=789
      f = function() return x end --global f
      print(f()); 
      x=123; 
      print(f());
end

fie() -- will print 789 newline 123

Apparently, you can still change the contents of 
the closure's x whilst the local x is in scope,
because they are the same x. Which makes sense 
due to the lexical scoping.


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