lua-users home
lua-l archive

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


On 17 Jan 2005 13:10:08 +1100
skaller <skaller@users.sourceforge.net> wrote:

> On Mon, 2005-01-17 at 04:17, Jay Carlson wrote:
> 
> > How do you refer to object slots defined by parents?
> 
> Inside the derived class it is easy:
> 
> function derived (x)
>   methods = base(x)
>   --- this is the parent table
> 
>   super_meth1 = methods.meth1
>   -- parent meth1
> 
>   methods.meth1 = 
>     function 
>        ... super_meth1 (x) ...
>        --- calls parent meth1
>     end
>   -- override meth1
>   ...

But how would you refer to a superclass's private instance variables? 
'x' in your example is sort of a special case, since it's passed in the
constructor.  What if base() defined a local y for its own use, and you
wanted to get at that?

It seems to come down to this:

K is an object.
K has some state.
K doesn't want things that use K to mess with K's state without K's
consent (whether this is done with accessor methods or metatables is
irrelevant.)
But subclasses of K sometimes need to mess with K's state directly in
order to extend K in some way.

But how?

I don't think this is a Lua-specific question at this point; any OO
language has to deal with this dilemma somehow.

The Lua way, as mentioned in (I think) PiL, is for K to just make its
state public and ask nicely that clients of K to please just not touch
it.

While it doesn't appeal to the paranoid side of me that wants some
assurance of isolation between components, I can see a certain
minimalist elegance in it, befitting a language like Lua.

-Chris