lua-users home
lua-l archive

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


Oh,

now I see better. You were talking about
tab.test = nil, not return tab.test (which is nil)
But the anwser is simple too:
You set tab.test = 123, calling your metatable's __newindex.
Then you set it to nil, as it existing, __newindex isn_t called.
Becasue it got nil, it disappeared from the table.
The next table.test = nil calls __newindex() as tab.test is now new.

--
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