Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!uxc!uxc.cso.uiuc.edu!m.cs.uiuc.edu!p.cs.uiuc.edu!johnson From: johnson@p.cs.uiuc.edu Newsgroups: comp.lang.smalltalk Subject: Re: Variable scoping in Smalltalk Message-ID: <80500050@p.cs.uiuc.edu> Date: 29 Mar 89 15:13:00 GMT References: <935@cs-spool.calgary.UUCP> Lines: 41 Nf-ID: #R:cs-spool.calgary.UUCP:935:p.cs.uiuc.edu:80500050:000:1895 Nf-From: p.cs.uiuc.edu!johnson Mar 29 09:13:00 1989 From Ken Warner: >How does one find out the super-class so that one can access the super >class-pool variables without explicitly stateing the name of the super class? Various people answered that "self class superclass" would give you the superclass of an object's class. This might be the direct answer, but it might not be answering the real question. I don't see why there can be any problem in accessing pool variables, and wonder if Ken isn't trying to access some other kind of variables. Subclasses use the exact same copies of class variables as the superclass that defines the variables. I think that pool variables are also inherited, so they shouldn't be a problem either. Therefore, I think that Ken must be talking about class instance variables. Class variables are really just global variables whose name is only accessible to a particular class and its subclasses. However, since classes are objects, they have instance variables, and class instance variables are instance variables declared specially for a class. Subclasses inherit the class instance variable definitions, but each class object has its own copy of the variable. If you want to access the instance variable of a superclass then you have to send a message to the superclass. However, which superclass? It might reallly be defined in the superclass of your superclass's supeclass, and you should use a technique that won't break when more classes are added. Here is a technique that is not too awful. Suppose that class X defines a class instance variable v. Define a class method in X to access v by v self == X ifTrue: [^v] ifFalse: [^self superclass v] This method is a little ugly because it accesses X directly, but then it is defined in X and only has to be defined once, so it isn't all that bad. It is much better than writing code in dozens of places that accesses X. Ralph Johnson