lua-users home
lua-l archive

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


Hi,

in the following code, with Lua 5.2.2...

a = {}
setmetatable(a, a)
a.__tostring = function() return "a tostring" end
a.__gc = function(t) print("a gc") end
print("table:", a)
a = nil
collectgarbage("collect")
print("=====")

b = {}
b.__tostring = function() return "b tostring" end
b.__gc = function(t) print("b gc") end
setmetatable(b, b)
print("table:", b)
b = nil
collectgarbage("collect")
print("=====")

...the gc is triggered only for table b. But the __tostring metamethod
works for both a and b. So somehow __gc and __tostring differ regarding
when the function setmetatable() is called. Why is this so?

Regards, Hartmut