[comp.windows.news] InstanceVars and classes, the real story

bmacinty@mud.uwaterloo.ca (Blair MacIntyre) (06/05/91)

Ok, so here's what was screwing me up (well, at least regarding this
particular problem!)  Hopefully, you won't get caught by this too!

Let's say I have classes A, B and C.  C inherits from B which inherits
from A.  

Now, lets say I'm modifying A.  Among other things, I add an instance
variable /blat.  I then do the following:
systemdict begin
<insert code for A>
end
which deposits the new definition of A in the systemdict.  Great.

The next time I create an instance of B or C, however, the instance
variable /blat is not defined.  "What?!?", I say.

I've basically been ignoring the problem and initializing my instance
variables in NewInit.  Today I decided to find out what was really
causing the problem, because it is so much neater and meaningfull to
initialize them at the top of the class definition.  Looking at
class.ps, we see that when a class is created, it's InstanceVars
dictionary is created by merging all it's superclasses InstanceVars and
it's instance variable dictionary.

So, _of_course_ the above fails.  The InstanceVars for B and C are
decided upon when the B and C classes are _created_, not _instantiated_!

Of course, now it makes perfectly good sence and is an obvious
optimization for class instantiation.  What bugs me is that it didn't
occur to me earlier!
--
Blair MacIntyre, Computer Graphics Lab
Dept. of Computer Science, University of Waterloo, Waterloo, ON, Canada, N2L3G1
{bmacintyre@{watcgl|violet|watdragon}}.{waterloo.edu|uwaterloo.ca}