[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: __assign
- From: "Virgil Smith" <Virgil@...>
- Date: Mon, 14 Jun 2004 10:51:37 -0500
You have to use __newindex for assignments and __index for reads.
Furthermore, if you want to catch accesses to already "existing" entries you
have to use 2 tables. One can contain whatever assigned values you wish to
keep, and the other remains empty so that its __newindex and __index methods
continue to be called. Of course the __newindex and __index methods will
have to access the table that actually holds the previously assigned values.
[mailto:email@example.com]On Behalf Of Dan East
Sent: Monday, June 14, 2004 9:22 AM
To: Lua list
Subject: RE: __assign
I'm afraid I don't have anything in print regarding Lua.
Thanks for the suggestion - this sort of works. I did a quick test, and
my __newindex handler is called when a non-existent element is assigned a
value, but not when it is read:
x=MyClass.x --Does not trigger __newindex handler
MyClass.x=10 --Triggers __newindex handler
So how is reading values handled in the proxy table?
So at this point I can catch writes, or let the user read, but not both at
[mailto:firstname.lastname@example.org]On Behalf Of Aaron Brown
Sent: Monday, June 14, 2004 11:02 AM
To: Lua list
Subject: Re: __assign
Dan East wrote:
> Is there some non-trivial implementation issue that
> prevents an __assign metatable event within Lua? Or is
> there some other mechanism already providing the same
It can currently be done with __newindex and a proxy table.
I don't see this explained in the wiki anywhere, but it is
on page 115 of Programming in Lua, if you have it.
Briefly, the idea is that the table the user assigns to is
always empty -- its __newindex metamethod does any
housekeeping you want done on every assignment, plus the
actual assignment to the real table (which the user never