[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- 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.
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]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
once. :)
Dan East
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]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
> functionality?
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
directly accesses).
--
Aaron