Hi ā
After a lot of discussion about this, it seems clear to me that the point
of instance local variables isnāt about privacy so much as preventing
accidental collisions. That calls for a solution that makes it easy
to access parentsā instance locals, while still differentiating their
namespaces. The proposed use of ānamespaceā seems a bit syntactically
heavy, though. So, Iām going to propose something analogous to the
case of Foo versus ::Foo:
@a instance local
@::a parent's instance local
@Foo::a ancestor-named-Foo's instance local
It doesnāt add anything new, since :: already exists for similar things.
Itās lightweight and non-restrictive, and seems to blend into the
language pretty well to me.
Youāre right that :: already exists in the language, but I think
thereās a big difference between using, say, A::x or A::X to get at a
class method or constant, and using @A::x to get at an instance
variable that happens to have been used in methods defined in A ā
namely, that A::x and A::X are documented. Whoeverās writing the
class definition of A is not going to treat @x (and shouldnāt have to
treat it) like part of the public interface, for purposes of
documentation, version-number adjustment for incompatible releases,
etc.
Hereās an example of breakage under innocent refactoring:
class A
def learn; @speech ||= āHelloā; end
def talk; puts @speech; end
end
class B < A
end
class C < B
def yell; puts @A::speech.uppercase
end
Then we (or some library writer) decides to do this instead:
class Speaker; def learnā¦ def talkā¦ end
class A < Speaker; end
etc.
So now the variable is @Speaker::speech, so the definition of yell
doesnāt work.
One could say: the solution is to study the code youāre inheriting
from. In that case, though, name collision isnāt going to be an
issue. But that, too, really unencapsulates things.
David
Ā·Ā·Ā·
On Sun, 7 Dec 2003, Jesse Tov wrote:
ā
David A. Black
dblack@wobblini.net