[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: __write v. a protected mode flag
- From: Mark Hamburg <mhamburg@...>
- Date: Fri, 02 Jan 2004 19:14:21 -0800
As I noted in my earlier message, I implemented a __write metamethod for
tables that works like __newindex but always gets called. (I've now learned
was called __set in Sol.) Implementation was impressively easy.
The benefits of this relative to using proxy tables are that the tables
continue to work like normal tables for reading and we avoid allocating an
extra table or two for the proxy. The proxy approach is cool, but as
discussions on this list have indicated, making it really work turns out to
be a somewhat long and complicated process that seems mostly to be driven by
finding things that no longer work correctly.
One problem is that it means anything wanting to do further work on a
metatable now has to account for the semantics of having a __write
metamethod or a __newindex metamethod. Furthermore, I kept the __newindex
metamethod for userdata since no change was needed there.
An alternative implementation would be to add another magic character into
the __mode field. For example "w" or "p" for write-protected. This looks
relatively easy but means that any weak tables will trigger a search of the
string when writing to them (probably not a big deal) and that any
write-protected tables will result in the garbage collector doing more work
to determine whether or not they indicate weak tables (possibly a
significant detail depending on how common write-protected tables are).
Any opinions on which approach seems better?
A further issue with either approach is that table.insert, etc. all use raw
access and hence don't go through the write function. One probably wouldn't
want them to in general, but they should perhaps look for metamethods on the
table that provide appropriate replacements.