[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Metatables are "hard" (Re: Question about __index and classes)
- From: Mark Hamburg <mark@...>
- Date: Sat, 10 Sep 2011 12:12:54 -0700
On Sep 1, 2011, at 10:56 AM, Tony Finch wrote:
> Mark Hamburg <email@example.com> wrote:
>> This thread plays a bit to a message I started a while ago and never
>> finished. Metatables are "hard" -- particularly for people coming from
>> backgrounds in OOP languages.
> I agree with what you said but you missed out one important point: the way
> that methods of the obj:meth(args) kind are looked up is completely
> different from the way metamethods are looked up. Other languages tend to
> make user-defined and language-level methods look the same.
Agreed. I think that had been on my earlier drafts and got lost in the final write up.
To make matters more confusing for the new-to-Lua programmer, people often implement methods using the __index entry in metatables, possibly going so far as to make __index point at the metatable itself (a horrendous semantic security hole if one worries about such things).
Newbie: So, to make + work for my custom type, I want an __add method?
Oldie: No. You want an __add metamethod?
Newbie: So, I put my methods in the metatable?
Oldie: But you can if you make the __index entry in the metatable point to the metatable.
Newbie: I don't really understand, but okay. (And now our newbie goes on not really understanding the difference between metamethods and methods until the problem rears up again...)