lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2015-04-15 2:42 GMT+02:00 Soni L. <fakedme@gmail.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
>> table.
> 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.