If you know about debug.setmetatable, you can leave now. If you didn't here it is some nice things you can do with it:
That's it bye.
Code:
COUNTER = 1
function toTable(object, name)
if not name then
name = COUNTER
COUNTER = COUNTER+1
end
_G["__" .. name] = _G["__" .. name] or {__name = name}
debug.setmetatable(object, {__newindex = function (a, b, c) rawset(_G["__" .. name], b, c) end, __index = function(a, b) return rawget(_G["__" .. name], b) end, __len = function() return #_G["__" .. name] end})
return object
end
function getTable(object)
if object.__name and _G["__" .. object.__name] then
return _G["__" .. object.__name]
end
end
local a = 10
toTable(a)
debug.getmetatable(a).__call = function() print("Why are you calling a number value? XD") end
a[1] = 2
a[2] = 4
a[3] = 6
print(a) -- 10
print(a[1], a[2], a[3]) -- 2 4 6
print(table.concat(getTable(a), ", ")) -- 2, 4, 6
a() -- Why are you calling a number value? XD
That's it bye.