lua-users home
lua-l archive

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


Marco Kögler escribió:

> Obviously, the table testObj did not contain a Hello-function.

Nope, and your __index metamethod didn't provide one either. So when
you try to call it, Lua complains.

> But, I thought if the index event handles this, then Lua would be silent.

The __index metamethod handles the table lookup; it is supposed to return
the "value" of the lookup, which is what will be called in your example.
The lookup works, as you note. In fact, you could have just said:

testObj.hello

and it would have worked.

If you wanted to make the testObj:hello() syntax work, you need the __index
metamethod
to return a function closure, which will then be called.

In C++ terms, the __index method is (sort of) like overriding the ->
operator, not
the () operator. Except that it isn't really -- you'll have to read the
manual, I'm
afraid. :)

Rici