[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: __call and metatables
- From: Remo Dentato <rdentato@...>
- Date: Fri, 13 Jan 2006 15:49:56 +0100 (CET)
I didn't fully understand Lua metatable mechanism.
I tought that the following example would allow me to use a table as if it was a function (note that there are two metatables):
mmt = {}
mmt.__index = mmt
function mmt.__call(tab,...)
print("called: ",tab,unpack(arg))
end
mt = {}
mt.__index = mmt
setmetatable(mt,mmt)
tab = {}
setmetatable(tab,mt)
Now, even if getmetatable(tab).__call is a function the instruction tab("xx") will fail. Reading the reference manual more carefully I noticed it states that "access to a metamethod does not invoke other metamethods" and that this is the expected behaviour.
What I don't understand is the reason why. Efficiency? I would have found this feature very useful.
remo.D
P.S. The same goes for __add etc.