lua-users home
lua-l archive

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

	Hi Michael

On Sat, 27 Aug 2011, Michael Kirsch wrote:
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.
	No you are wrong.  Both trigger metamethods.  The "." is syntatic
sugar for string (*) key indexing, while the ":" is syntatic sugar for
an indexing and a call using the table as the first argument.

t = {...}
t["bla"] == t.bla
t:f(arg1) == t.f(t, arg1) == t["f"](t, arg1)

so, then why is it necessary to say:

   MyClass.__index = MyClass

for you to be able to use ":"?
	It is not.

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.
	I think you are mixing things...  Sorry but I cannot follow you...


(*) My assertion is not quite correct.  The string key might be lexically
an identifier.