lua-users home
lua-l archive

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


Because __newindex handles assignments.
You are doing a lookup; for this __index() would be relevant.

--
Oliver

Am 22.09.2015 um 17:41 schrieb Scott Morgan:
Seeing the following behaviour on various version of Lua:

Lua 5.2.2  Copyright (C) 1994-2013 Lua.org, PUC-Rio
~ mt = { __newindex = function(t,k,v)
~~ print("Setting :", k, "to", v)
~~ rawset(t,k,v)
~~ end }
~ tab = setmetatable({},mt)
~ return tab
table: 0x671df0
~ return tab.test
nil
~ tab.test = 123
Setting :	test	to	123
~ tab.test = nil
~ tab.test = nil
Setting :	test	to	nil
~ tab.test = nil
Setting :	test	to	nil
~ tab.test = 321
Setting :	test	to	321
~ return tab.test
321
~ tab.test = nil
~ return tab.test
nil
~ tab.test = nil
Setting :	test	to	nil

Why isn't the first `tab.test = nil` event being handled by the
metatable? As mentioned, I'm seeing this across Lua versions, 5.1, 5.2
(as above) and 5.3 (and different OS's/compilers)

It's weird because, surely, if this is a bug, it has already been seen.
It's such a common pattern. I must be doing something wrong here?

Scott