lua-users home
lua-l archive

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


On 17 Jan 2005 14:52:45 +1100
skaller <skaller@users.sourceforge.net> wrote:

> On Mon, 2005-01-17 at 14:10, Chris Pressey wrote:
> > 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?
> 
> This is harder to do, although probably possible.

Possibly.  But I've yet to think of or read of a way that doesn't use
the debug interface.

> > I don't think this is a Lua-specific question at this point; any OO
> > language has to deal with this dilemma somehow.
> 
> I agree, it isn't Lua specific.
> 
> > 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.
> 
> No, that sucks. That isn't a solution.

I'm not sure anymore.  Outside code that is determined to abuse my class
can do it anyway, so I'm mainly concerned with preventing accidental
misuse of my state.  If, for example, some kind of linter tool could
look at my code and tell me where clients are using my state directly,
and I can compare that with a list of known clients who I deem allowed
to do this, then it would be perfectly fine for me to make it all
public.

...

On Mon, 17 Jan 2005 15:22:39 +0100
Klaus Ripke <paul-lua@malete.org> wrote:

> On Monday 17 January 2005 04:10, Chris Pressey wrote:
> > But how would you refer to a superclass's private instance
> > variables?
> you meant what is usually called "protected"?

Yes.

> > What if base() defined a local y for its own use, and you
> > wanted to get at that?
> well, you don't

OK.  That would diminish the possibilities for extending the class in
the future, and thus be "bad".

[...]
> Often enough I saw people subclassing something just to get access
> to it's ``protected´´ state, which is ridiculous and self-defeating
> (not to mention "final subclasses" as proposed antidote).

Yes.

This all suggests to me that the idea that Lua's "just please don't
touch" policy really is a reasonable solution.  And in light of it, I'll
probably start moving away from a closure-based model to a table-based
model in my own code in the coming days.

Thanks,
-Chris