lua-users home
lua-l archive

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


On 2011-Apr-27 (Wed) at 10:52 (+0400), Alexander Gladysh wrote:

>> I often see that people do miss the fact that __index and __newindex
>> are called for non-existant keys only. When I was a newbie I missed
>> that too.

Maybe the underlying problem is that the names __index and __newindex
are poorly chosen.  They seem inconsistent on two levels:

-- Compared to all other metamethod names:

   Take for example __add and __concat : these apply to *all* uses
   of '+' and '..'.

   However, __index and __newindex apply to just *some* uses of '[ ]',
   namely those for non-existent keys.

-- Amongst themselves:

   Both __index and __newindex apply to 'new' (i.e. non-existent) keys,
   but only one of these names contains the word 'new'.

Surely names such as __getnew and __setnew would be much clearer.

The 'get' and 'set' words make their actions much more obvious,
as classic getters and setters, and the word 'new' suggests that
they're dealing with new keys.

Also, they align better with metamethod names such as __getmetatable
and __setmetatable.

Perhaps these could be considered for future versions of Lua?

Unlike contentious issues such as tables with holes, or __ENV,
such a change would have no semantic effect whatsoever, and be
just a minimal syntactic side-step.  To avoid breakage of all
exiting programs using __index and __newindex, those older names
could be kept around but deprecated; a simple find/sed script
(in *nix) could change all Lua source to use the new names.

------------------------------------------------------------------------
Joseph Manning / Computer Science / UCC Cork Ireland / manning@cs.ucc.ie
------------------------------------------------------------------------