[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Why do we not have an __unindex metamethod?
- From: Dirk Laurie <dirk.laurie@...>
- Date: Wed, 15 Apr 2015 07:54:33 +0200
2015-04-15 2:42 GMT+02:00 Soni L. <email@example.com>:
> On 14/04/15 08:58 PM, Luiz Henrique de Figueiredo wrote:
>>> Why do we not have an __unindex metamethod, that gets called when a
>>> key gets set to nil (either by GC or manually (including rawset))?
>> If you need control over set and get events for a table, use a proxy
> We have __gc already, I don't think anyone would mind another "special"
> metamethod. (__unindex would be kinda like __gc but for table keys)
Why do we not have
... a million dollars?
... the gift to see ourselves as others see us?
... the faith that moves mountains?
Let's rephrase the question.
Would it be worth the extra overhead to make provision for
an __unindex metamethod, that gets called when a key gets set
to nil (either by GC or manually (including rawset))?
Is it really "kinda like __gc"?
The check for __gc. is made only once in the lifetime of
a value. The cost is negligible.
A check for __unindex would be triggered by the assignment
of nil. There is nothing special about assigning nil: for example,
it is perfectly OK to assign nil to a table key during a traversal
controlled by "next", as in a loop over "pairs". There is no need
for the implementation to make a test for nil.