[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua5.0 new "For" question
- From: Eero Pajarre <epajarre@...>
- Date: Sun, 22 Dec 2002 23:16:52 +0200
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:
f = function() return x end --global f
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.
You can change it even later ;-)
function f() return x end
function g(y) x=y end
print(f()); --prints 123
print(f()); --prints 321
This is powerfull stuff....
(Were upvalues read-only?)
Btw. I became curious about what Peter had suggested
year ago. I think that what he wanted (protected datatypes
in Lua) might be implementable with closures. Create an object
which has its internals in a closure, which can only be
accessed by the interface functions, hmmm, I don't know if there
are any holes here.