lua-users home
lua-l archive

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


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