[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: __mul()
- From: Mark Hamburg <mark@...>
- Date: Sat, 20 Mar 2010 22:46:49 -0700
On Mar 20, 2010, at 10:44 PM, Mark Hamburg wrote:
> See the specification for the __mul operator in the Lua spec (section 2.8).
>
> The implementation of the * operator looks in the metatable for a __mul entry. It does not perform a message send. It does not index the object. Once it finds the entry, it passes it the two operands. In other words, it does more or less exactly this:
>
> local temp = bot:getLoc()
> newpt = getmetatable(temp).__mul(temp, 2)
>
> On the other hand, the method call approach:
>
> newpt = bot:getLoc():__mul(2)
>
> Is doing this:
>
> local temp = bot:getLoc()
> newpt = temp.__mul(temp, 2)
>
> This won't look in the metatable. It will look in the index table of the metatable, but that's a different matter unless the metatable points to itself via the __index entry -- i.e., it's use of the metatable if the object doesn't have a __mul entry looks like the following assuming that __index leads to a table:
>
> local temp = bot:getLoc()
> newpt = getmetatable(temp).__index.__mul(temp, 2)
>
> Mark
And yes, this does take some effort to get one's head around if one comes from an object-oriented background where all of this metatable-based behavior would be handled via methods on objects.
Mark