From: "T. Onoma" Date: 2003-12-06T04:47:26+09:00 Subject: Re: Attempted roadmap of future instance variables.... On Friday 05 December 2003 07:40 pm, Nathaniel Talbott wrote: > The core of the problem that class local instance variables are trying > to solve is variable shadowing (as Guy so elegantly showed). The > problem with making all variables class local and using accessors, is > that if I am shadowing an instance variable, there is a good chance > that I will shadow the accessor as well. The superclass variable thus > becomes completely inaccessible: I see what you're saying, but I would think you would know that you were doing this if you were subclassing, just as you know it when you want to redefine a method of the superclass. > # pretend all instance variables are class local > class A > attr_accessor :a > > def initialize > @a = 1 > end > end > > class B < A > attr_accessor :a > > def initialize > @a = 'a' > end > end > > There is no way for the subclass to access the superclass's instance > variable @a now, and I don't see a good work-around for this. So it would seem to me, that in fact, this is an example of one actually wanting to redefine the accessor to cut the scope off at the subclass, so that @a of the superclass could no longer be accessed. A purposeful act. > I wonder if a better solution for the issue doesn't lie in the idea of > namespaces. Basically, library writers want to be able to say that > @checkpoint is part of the Transaction::Simple namespace, and > occasionally library users want to say that they want to read or write > @checkpoint in the Transaction::Simple namespace. If there was a way to > explicitly limit the namespace of an instance variable to a given Class > or Module, and a way to access an instance variable in an explicit > Class or Module, that would be quite handy. The key is that all data > remains accessible all the time, to everyone. We just allow folks the > ability to shadow. You can always just augment the superclass/module to give you the access you need: module Transaction::Simple def simple_checkpoint @checkpoint end end T.