lua-users home
lua-l archive

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

于 2011-8-28 6:52, Michael Kirsch 写道:
There is something I don't understand about the way __index and
metamethods work. The way I understand, the "." operator looks in the
table itself and the ":" operator looks directly in the metatable. If
so, then why is it necessary to say:

     MyClass.__index = MyClass

for you to be able to use ":"?

If ":" looks right in the metatable, then it shouldn't be needed. To
me it seems like what it would do is let you call metamethods using
the "." operator, but it doesn't work that way for some reason.

many people have given the right answer,

I would just say one more setence.
although the `colon call' is said to be a syntactic sugar, it is in fact treated not exactly the same way
as the 'dot index' in that the `colon call' evaluate the `prefixexp' only ONCE,
and the lvm has an dedicated opcode (OP_SELF) specially for the `colon call'.