The Lua 5.3 Reference Manual (in this part, scroll down) says:
__newindex: The indexing assignment table[key] = value. Like the index event, this event happens when table is not a table or when key is not present in table. The metamethod is looked up in table.
However I don't understand that the metamethod __newindex happens when table is not a table. What does that mean? I did try to re-assign a local with nil, but it didn't work (yes, I know it doesn't make sense to re-assign the table, but this would help it to be garbage-collected).
local v = {};
setmetatable(v, {
__newindex = function(t,k,v)
print("Aaahhh...!");
end
});
v = nil;
I'm using this online compiler to test it.
From the page you cited:
You can replace the metatable of tables using the setmetatable
function. You cannot change the metatable of other types from Lua code
(except by using the debug library (ยง6.10)); you should use the C API
for that.
You can't use setmetatable to change the metatable for something that's not a table, so you won't be able to verify what you expect (that the __newindex method is called when you index into something that's not a table).
Your code runs because you are setting the metatable for a table. (local v = {} creates a table.)
But reassigning the variable v to be something else means you no longer have a way to get to the table you created. If your last line were v[5] = 'Hello', then you would see that your metamethod is invoked.
EDIT
Reading your edit, it looks like you expected __newindex to get called when the table was garbage collected? I think you misunderstood the statement that __newindex is called "when table is not a table." That means if you did something like this:
local v = 5
print(v[3]) -- indexing into something that's not a table
__newindex would be called (because in the expression table[key], which here is v[3], table is not a table). But you can't actually set up __newindex via the setmetatable method, because that method only works on tables.
Related
Lua doesn't offer a unique way to OOP.
With setmetatable many alternatives are possible.
Here's what I tried:
Person={}
function Person.__call(cls,name)
return setmetatable({name=name},cls)
end
function Person:say(what)
print(self.name..'> '..what)
end
setmetatable(Person,Person)
p=Person('Fred')
p:say('hello') -- 18
which gives the error:
18: attempt to call a nil value (method 'say')
I can add:
function Person.__index(cls,k)
return Person[k]
end
and then the above code works correctly, however I do not understand why the method is not found when Person is already metatable of itself.
You need to implement the __index metavalue in order to relay indexing access operations. Having a metatable alone is not sufficient.
Also note that it is recommended to implement all meta methods befor using a table as a metatable.
Refer to the Lua 5.4 Reference Manual 2.4 Metatables and Metamethods
__index: The indexing access operation table[key]. This event happens when table is not a table or when key is not present in table. The
metavalue is looked up in the metatable of table.
The metavalue for this event can be either a function, a table, or any
value with an __index metavalue. If it is a function, it is called
with table and key as arguments, and the result of the call (adjusted
to one value) is the result of the operation. Otherwise, the final
result is the result of indexing this metavalue with key. This
indexing is regular, not raw, and therefore can trigger another
__index metavalue.
__index is that metavalue. So if you don't provide that metavalue, what should Lua do?
In the following example that metavalue is Person.
So when I call a:sayName(), Lua will find that a.sayName is nil. It will check if in a's metatable Person there is a __index metavalue. There is and in this case it's a table named Person so it will index that person with key "sayName" which results in the following function call: Person["sayName"](a)
local Person= {}
Person.__index = Person
setmetatable(Person, {
__call = function (cls, ...)
return cls:_init(...)
end,
})
function Person:_init(name)
local o= setmetatable({}, self)
o.name = name
return o
end
function Person:sayName()
print(self.name)
end
local a = Person("Lisa")
a:sayName()
The index metamethod can be set equal to tables. From what I can tell
foo.__index = function(self, k)
return bar[k]
end
and
foo.__index = bar
are the same. Why is declaring functions this way allowed in this situation?
This isn't a function declaration - assigning a table to __index is just a shortcut for using the function that you described.
From Programming in Lua (for Lua 5.0, but this part of the language hasn't changed):
The use of the __index metamethod for inheritance is so common that
Lua provides a shortcut. Despite the name, the __index metamethod does
not need to be a function: It can be a table, instead. When it is a
function, Lua calls it with the table and the absent key as its
arguments. When it is a table, Lua redoes the access in that table.
It's not like the table that you assign magically becomes a function. type(foo.__index) will still return table, and you can still do things with it that you can do with other tables, like using pairs and next, etc.
I am looking for a metamethod (or a workaround) that fires when removing an element from a lua table similar to the __newindex metamethod.
Ideally it would work something like the following:
local mytable = {}
local mt = {
__newindex = function(t,k,v)
rawset(t,k,v)
-- some other functionality
end,
-- This does not exist
__remove = function(t,k)
--some functionality
end
}
setmetatable(mytable,mt)
-- __newindex fires
mytable["key"] = value
-- __remove fires
mytable["key"] = nil
I have tried working with the __gc metamethod but that is not usable in this implementation due to the fact that the metamethod only triggers when the garbage collection cycle happens. I have no control over the garbage collection because the table (with the metamethods) is passed to a different script.
Possible workaround - do not store actual data within table.
Let your mytable act as a proxy, and store actual values in some shadow table. It might be allocated along with mytable, or data can be stored directly in metatable (so metatable must be created per mytable instance).
Here's example (easily broken by writing data under metamethods' name keys, but you get an idea), data will be stored within metatable:
http://ideone.com/eCOal3
local mytable = {}
local mt = {}
function mt.__newindex(t,k,new_value)
local previous_value = mt[k]
rawset(mt,k,new_value)
if previous_value and new_value == nil then
print "__remove() triggered"
end
end
mt.__index = mt
setmetatable(mytable, mt)
mytable.key = 123
print(mytable.key)
mytable.key = nil
print(mytable.key)
As assigning nil fires not metamethod at all, you will have to resort to an explicit removal function that does whatever you wanted the metamethod to do and then assign nil to the table entry.
I want to "unhook" a metatable from a table and was wondering if:
tbl = setmetatable(tbl, false) -- or nil
is the correct way to do this? I could not find any info about how to do it correctly. Do I need to use an assignment operator?
Also, would this be enough to destroy the metatable attached to the table if the metatable never had a reference and was anonymous?:
tbl = setmetatable({}, {__index = something})
-- later on:
tbl = nil
and the garbage collector would be enough to remove both tables?
According to the Lua reference, which you should always consult befor putting up a question here, setmetatable(tbl, nil) will delete the metatable of table tbl unless tbl's original metatable is protected. Or let's better say it does not delete the metatable but the reference to it. The table that served as metatable will of course not be deleted as long as there are other references to it.
Befor you ask people if a simple function call works, try it yourself.
You can use https://www.lua.org/cgi-bin/demo or any other Lua interpreter and you get your answer in seconds without involving anyone else.
Running this code:
setmetatable({}, false)
or
setmetatable({})
will result in
input:1: bad argument #2 to 'setmetatable' (nil or table expected)
Now you know that you cannot enter false and you have to enter nil explicitly.
To checkout that __metatable thing you would have read in the reference manual you could then try this code
local tbl = setmetatable({}, {__metatable = true})
setmetatable(tbl, nil)
Which results in the following output:
input:2: cannot change a protected metatable
To the second part of your question:
tbl = nil will not delte the table referenced by tbl. It will only remove the reference tbl to it.
local a = {}
local b = a
b = nil
print(a)
a is still a table. You only removed one of its references.
Once there is no reference left the garbage collector may collect the table.
setmetatable(tbl, {}) will establish a reference to the table returned by the table constructor {} and store that reference somewhere in the guts of tbl.
If tbl was the last reference to that table it will be collected as garbage at some point. Then of course the only reference to the table you set as metatable will also be gone and it will removed as well.
If you do something like that:
local a = {}
local b = setmetatable({}, a)
,
a = nil will not delete b's metatable
So yes it will remove both tables if no other reference to either one of them is left.
__index = function(tbl, key)
local a = tbl[key]
if a <=0 then a = 0 end
if a > 5 then a = 0 end
return a
end
Book says:
Though the preceding code looks very innocent and tries to keep the value of the element in the table within a range, this code will cause problems and circular references. The first line in the function, a = tbl[key], will actually trigger another index function call, and that in turn will invoke another, and so on.
But how is a = tbl[key] calling another index at every call ?
That is a bit wierd thing to do. Lua triggers __index metamethod only if it can't find a field in the table. Therefore, using tbl[key] inside it makes absolutely no sense at all. Unless tbl is not a table.
Anyhow, if you want to access a table's field from within __index, use rawget. That will ensure that no metamethods are called.
EDIT:
Let me explain how __index lookup works:
Let's assume the table has a metatable with __index defined.
If Lua can't find the key in the table, it looks for the __index field in the metatable. It does not look for the key itself. Then, if the __index is a table, it looks for the key in that table (not the metatable, although it is common to associate the metatable itself with its __index field). If it is a function, it is invoked with two parameters: table (initial table, not the metatable) and the key.
So if the __index metamethod is called, you can be certain that the initial table does not have that field defined. Therefore, when you try to index it again (as the first argument is the original table that triggered the index lookup), the story begins anew -> Lua can't find the key, it invokes the __index and so on.