[comp.windows.news] Class vars or Instance vars, conclusion

knut-skog%rglab.uit.uninett@NORUNIX.BITNET (Knut Skog) (03/14/89)

My comments on this issue may have propagated some of
my own confution.  My current understanding is
that the values of composite instance variables are
shared dispite being an "instance".

This is demonstrated by the following example;

% The test should be executed incrementaly in three steps as indicated.

% Step1: Make your own subclass
/MyClass DefaultWindow
   dictbegin
     /InstComposite (Instance string) def
   dictend
  classbegin
    /ClassComposite (Class string) def
    /PaintClient {
                /Helvetica-Bold findfont 18 scalefont setfont
                100 100 moveto
          InstComposite show
          100 50 moveto
          ClassComposite show } def
  classend def
/MyCInst1 framebuffer /new MyClass send def
{ /FrameLabel (Inst 1) def } MyCInst1 send
  /reshapefromuser MyCInst1 send
  /map MyCInst1 send
% end Step1
%  Step2: Change Instance value.
 { /InstComposite ClassComposite def % Some dict or an array assignment is
                                                          % more likely in real
   }  MyCInst1 send
% end Step 2.
%  Resize window to see the to strings being equal.
%  Now we believe that our instance , its values inclusive,
%  is non shared (traditional interpretation).

%   We  modify the composite value of our class object:
% Step3:
  { ClassComposite 0 188 put } MyClass send
% end Step3.

%  We have not done anything explicitly to MyCInst. However,
%  having instructed a change in our class definition, when it
%  is repainted, we see that the instance variable (InstComposite)
%  of our first instance  has been changed indirectly!

%  Hence composite values of instance vars are shared because composite
%  def-ing in PostScript is by reference (not by value).


Knut

gaynor@athos.rutgers.edu (Silver) (03/14/89)

> My comments on this issue may have propagated some of my own confution.  My
> current understanding is that the values of composite instance variables are
> shared dispite being an "instance".

Indeed, I refer you to page 27 of The Red Book.  This property is clearly
demonstrated by the following example:

  69 dup       % ... 69 69
  1 add        % ... 69 70

  [69] dup     % ... [69] [69]
  dup 0 70 put % ... [70] [70]

Regards, [Ag] gaynor@rutgers.edu

[BTW, confution --> confusion, dispite --> despite; yes, English sucks.]

mh@wlbr.EATON.COM (Mike Hoegeman) (03/27/89)

In article <102*knut-skog@rglab.uit.uninett> knut-skog%rglab.uit.uninett@NORUNIX.BITNET (Knut Skog) writes:
>
>My comments on this issue may have propagated some of
>my own confution.  My current understanding is
>that the values of composite instance variables are
>shared dispite being an "instance".
>

Just as an added note to this discussion, to make a non shared composite
instance variable for an object class just create it in the objects
"/new" method like this

/MyClass ParentObject
dictbegin
	% instance vars...
	% normally you would put MyNonSharedArrayInstanceVariable here
dictend
classbegin
	/new {
		/new super send begin
		   % we are the instance at this point...
		   /MyNonSharedArrayInstanceVariable [
			1 2 3 4 (five) (six) currenttime /whatever
		   ] def
		currentdict end
	} def
classend def

-mike