[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: New metamethod for method lookup
- From: Mark Feldman <mfeldman@...>
- Date: Wed, 18 Nov 2009 14:20:10 +1100
TNHarris wrote:
I'm less enthusiastic about the proposal today than yesterday. Perhaps
the difficulty with __index is to remind us that Lua is not an OO
language and we shouldn't be trying to make it one.
True, but there are some (relatively) minor changes that could be made
which would open up a lot of features available to other languages that
are more OO-friendly. One of the problems I've encountered with __index
is that it prevents you from efficiently implementing virtual data
members. Consider the case where "child" has a metatable "parent", and
"parent" has a metatable "grandparent". Grandparent contains an __index
function to catch any non-members that are accessed and handles them
accordingly. However, when grandparent.__index is called, the value of
self is set to parent, not child, which is totally contrary to the whole
concept of "self":
===
local grandparent = {type="grandparent", __index =function(self, _)
return self.type end}
local parent = setmetatable({type="parent"}, grandparent)
parent.__index = parent
child = setmetatable({type="child"}, parent)
print(child.foo) --> parent
===
The only way for grandparent to get access to the proper value of self
(i.e. child) is to also add an __index function to parent and pass it
down the chain explicitly, which immediately shadows all members of
grandparent from ever being called directly.
This is not a contrived example, it's a direct analogy to OOP cases
where grandparent is a base class, parent is a derived class and child
is an instance of derived. Derived (parent) has to __index itself so
that operators work properly on the instance, so the only way to
efficiently implement virtual data members in this case is to change the
VM so that either the value of self passed into settable remains
constant as you traverse the metatable chain, or pass that value in as a
third parameter into grandparent.__index. It's a negligable change to
the VM that only effects __index function calls (which are slow anyway)
but it would immediately add the ability to implement efficient virtual
data members to inheritence heirarchies, which is a very useful feature
of other OOP languages.
Mark Feldman
This message and its attachments may contain legally privileged or confidential information. This message is intended for the use of the individual or entity to which it is addressed. If you are not the addressee indicated in this message, or the employee or agent responsible for delivering the message to the intended recipient, you may not copy or deliver this message or its attachments to anyone. Rather, you should permanently delete this message and its attachments and kindly notify the sender by reply e-mail. Any content of this message and its attachments, which does not relate to the official business of the sending company must be taken not to have been sent or endorsed by the sending company or any of its related entities. No warranty is made that the e-mail or attachment(s) are free from computer virus or other defect.