[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: The __newindex metamethod
- From: HyperHacker <hyperhacker@...>
- Date: Mon, 10 Jan 2011 16:56:12 -0700
On Mon, Jan 10, 2011 at 14:59, Axel Kittenberger <axkibe@gmail.com> wrote:
> It's all about speed.
>
> Am 10.01.2011 20:28 schrieb "Dirk Laurie" <dpl@sun.ac.za>:
>
>>
>> The reference manual states:
>> --
>> "newindex": The indexing assignment table[key] = value.
>> --
>> The operation can be overridden by redefining __newindex
>> in the metatable. However, the overriding function is
>> only invoked when table[key] does not yet exist. Otherwise,
>> table[key]=value <==> rawset(table,key,value).
>>
>> This behaviour is documented in the reference manual by the
>> Lua code for settable_event,
>> local v = rawget(table, key)
>> if v ~= nil then rawset(table, key, value); return end
>> h = metatable(table).__newindex
>> if h == nil then rawset(table, key, value); return end
>> which corresponds closely to the C code for luaV_settable in lvm.c.
>>
>> I can't guess why the overriding function is not always invoked
>> when available, i.e. simply
>> h = metatable(table).__newindex
>> if h == nil then rawset(table, key, value); return end
>> unless it is because the user is not trusted to call rawset in
>> the function supplied, which could cause dangling references.
>>
>> In the meantime, if I want this simpler behaviour, it seems that
>> I will have to change the code in lvm.c and recompile Lua.
>>
>> Dirk
>>
>
There is a patch to do something like this (__usedindex), but I
imagine the performance hit is rather noticeable.
--
Sent from my toaster.