Lua 元表类析构函数
我有以下 Lua 元表类,如何向其添加析构函数,以便当特定条件到达时它将破坏创建的对象并将其值设置为 nil?
-------------------------------------------------
-- Arrow class
-------------------------------------------------
local arrow = {}
local arrow_mt = { __index = arrow } -- metatable
function arrow.new(x, y) -- constructor
local newArrow = {
position = { x = x, y = y }
}
return setmetatable( newArrow, arrow_mt )
end
function arrow:remove()
-- remove the object here
-- self = nil dosent work
end
I have the following Lua metatable class, how can I add a destructor to it so when a certain condition arrives it will destruct the created object and set it's value to nil?
-------------------------------------------------
-- Arrow class
-------------------------------------------------
local arrow = {}
local arrow_mt = { __index = arrow } -- metatable
function arrow.new(x, y) -- constructor
local newArrow = {
position = { x = x, y = y }
}
return setmetatable( newArrow, arrow_mt )
end
function arrow:remove()
-- remove the object here
-- self = nil dosent work
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Nicol 所说,一旦变量获得对对象的引用,对象本身就无法控制该变量(即将其值设置为nil)。实际上,这是一件好事 - 想象一下有人在某个地方保存了对局部变量的对象的引用。突然在一个未知的时刻,它变成了一个nil引用(因为它在其他地方被销毁了),并且任何进一步访问它都会导致错误。
你真的需要摧毁这个物体吗?为什么? Lua 垃圾收集器是否正确地完成了它的工作?难道就没有其他方法来设计对象之间的关系吗?
例如,在最简单的情况下,您可以通过
collectgarbage("collect")
强制进行垃圾回收。垃圾收集将清理所有没有强引用的对象。如果您确实希望变量消失,请将它们保存在弱表中。当然,当你分配对象时,Lua 会自动进行垃圾回收(除非你停止它)。您还可以修改垃圾收集的速度。As Nicol said, once a variable gets a reference to your object, the object itself cannot control the variable (i.e. set it's value to nil). Actually, this is a good thing - imagine someone somewhere saved a reference to you object to a local variable. Suddenly at an unknown moment, it becomes an nil reference (because it is destroyed somewhere else), and any further access to it results in an error.
Do you really need to destroy the object? Why? Isn't the Lua garbage collector doing it's job correctly? Isn't there another way to design the relationship between the objects?
For example, in the simplest case, you can force a garbage collection through
collectgarbage("collect")
. A garbage collection will clean all objects, that have no strong references to them. If you really want variables to disappear, keep them in a weak table. Of course, Lua will do the garbage collection automatically while you allocate objects (unless you stop it). You can also modify the speed of garbage collection.你不能。 Lua 不是 C/C++;而是 C/C++。它使用垃圾收集。因此你必须依赖垃圾收集;您的对象的用户可以控制它何时消失。当他们使用完它时,他们应该放弃对它的引用。
因此,虽然您可以有一个明确的“新”(尽管您不应该这样称呼它),但您不能有一个明确的“删除”。当对象不再被引用时,就会发生销毁。
You can't. Lua isn't C/C++; it uses garbage collection. You therefore have to rely on garbage collection; the user of your object has control over when it goes away. It is up to them to discard their references to it when they're done with it.
So while you can have an explicit "new" (though you shouldn't call it that), you don't get to have an explicit "delete". Destruction will happen when the object is no longer referenced.