在Lua中从表格中删除Metatables(Removing Metatables from a table in Lua)

编程入门 行业动态 更新时间:2024-10-27 13:30:17
在Lua中从表格中删除Metatables(Removing Metatables from a table in Lua)

我想从桌面上“解开”metatable,并想知道是否:

tbl = setmetatable(tbl, false) -- or nil

这是正确的方法吗? 我找不到任何有关如何正确执行此操作的信息。 我需要使用赋值运算符吗?

另外,如果metatable从未有过参考并且是匿名的,那么这是否足以摧毁附加在表上的metatable ?:

tbl = setmetatable({}, {__index = something}) -- later on: tbl = nil

和垃圾收集器将足以删除两个表?

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?

最满意答案

根据Lua的参考资料,你应该在这里提出一个问题, setmetatable(tbl, nil)会删除表tbl的元表,除非tbl的原始元表受到保护。 或者,我们最好说它不会删除metatable,而是对它的引用。 只要还有其他引用,表格作为metatable将不会被删除。

当你问人们一个简单的函数调用是否有效时,你可以自己尝试一下。 您可以使用https://www.lua.org/cgi-bin/demo或任何其他Lua口译员,并在几秒钟内得到答案,而不涉及任何其他人。

运行此代码:

setmetatable({}, false)

要么

setmetatable({})

会导致

输入:1:错误的参数#2到'setmetatable'(预期为零或表)

现在你知道你不能输入false,你必须明确输入nil。

要检出你在参考手册中读到的__metatable的东西,你可以试试这段代码

local tbl = setmetatable({}, {__metatable = true}) setmetatable(tbl, nil)

其结果如下:

输入:2:无法更改受保护的元表

对于你的问题的第二部分:

tbl = nil不会删除tbl引用的表。 它只会删除它的引用tbl。

local a = {} local b = a b = nil print(a)

a仍然是一张桌子。 您只删除了其中一个参考。

一旦没有引用留下垃圾收集器可能会收集表。

setmetatable(tbl, {})将建立对表构造函数{}返回的表的引用,并将该引用存储在tbl 。

如果tbl是该表的最后一个参考,它将在某个时刻被收集为垃圾。 那么当然,您设置为metatable的表的唯一引用也将消失,并且它也将被删除。

如果你做这样的事情:

local a = {} local b = setmetatable({}, a)

, a = nil不会删除b的metatable

所以是的,如果没有其他任何一个引用被留下,它将删除这两个表。

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.

更多推荐

本文发布于:2023-08-02 23:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382520.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表格   Lua   Metatables   table   Removing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!