lua-users home
lua-l archive

__index function in a metatable shared by several tables

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


Sorry for the vague subject. I don't know better words to summarize my question.
The following code prints
1
You called foo
Is there any way for the function f to modify o.test? In my code there could be multiple o tables, all sharing the t metatable. I would like function f to store state information in each individual o table.
t = {}
setmetatable(t, {__index =
 function(_, k)
 local function f()
 print("You called " .. k)
 end
 return f
 end
})
o = setmetatable({ test=1 }, t)
t.__index = t
print(o.test)
o.foo()

AltStyle によって変換されたページ (->オリジナル) /