Skip to content

Instantly share code, notes, and snippets.

@akirayu101
Created July 8, 2014 12:56
Show Gist options
  • Save akirayu101/bd92ece93bb6654087f3 to your computer and use it in GitHub Desktop.
Save akirayu101/bd92ece93bb6654087f3 to your computer and use it in GitHub Desktop.
method missing
hook_tb = {}
local function hook_index(o, key)
print("get", key)
if type(o["data"][key]) ~= "table" then
return o["data"][key]
else
local tb = hook_tb:create()
tb["data"] = o["data"][key]
return tb
end
end
local function hook_newindex(o, key, value)
print("set", key, value)
o["data"][key] = value
end
function hook_tb:create()
print("create tb")
local tb = {}
tb["data"] = {}
setmetatable(tb,{__index = hook_index,__newindex = hook_newindex })
return tb
end
t = hook_tb:create()
print(t.hello)
t.hello = 1
t.hello = {1,2,3}
print(t.hello[1])
local st = t.hello
print(st[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment