The following code should print 'hello', however it is printing the memory location of the table (i.e. 'table: 052E67D0'). Please, explain what I'm missing here.
TestClass = {}
function TestClass:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function TestClass:__tostring()
return "hello"
end
local t = TestClass.new{}
print(t)
Update
Tried doing this instead:
TestClass = {}
function TestClass:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
self.__tostring = function() return "hello" end
return o
end
local t = TestClass.new{}
print(t)
which worked. This seems weird because, to me, self
in constructor and TestClass:
refer to the same table.