[comp.lang.smalltalk] Question concerning behavior of assignment and copy methods

trr@rayssd.ray.com (Terry R. Raymond) (03/26/88)

I am using Smalltalk/V and would like to know if the following
methods behave the same way in Smalltalk-80.  The methods of
concern are; assignment, shallowCopy and deepCopy.  The expressions
below demonstrate their behavior.  Would somebody who uses
Smalltalk-80 please inform me if the methods behave the same way in
Smalltalk-80.  The expressions were evaluated with "Show it".

Assignment

| a b |
a := #(23 45 78).
b := a.
b at: 1 put: 93.
^a  

The result was  (93 45 78).

Shallow copy

| a b |
a := #(23 45 78).
b := a shallowCopy.
b at: 1 put: 93.
^a

The result was (23 45 78).

| a b |
a := #(23 (45 56) 78).
b := a shallowCopy.
(b at: 2) at: 1 put: 93.
^a
   
The result was (23 (93 56) 78).


Deep Copy

The first example was the same as shallowCopy.

| a b |
a := #(23 (45 56) 78).
b := a deepCopy.
(b at: 2) at: 1 put: 93.
^a
 
The result was (23 (45 56) 78).

| a b |
a := #(23 (45 (56 78) 91) 84).
b := a deepCopy.
((b at: 2) at: 2) at: 1 put: 99.
^a
 
The result was (23 (45 (99 78) 91) 84).


-- 
Terry Raymond
Raytheon Submarine Signal Division; Portsmouth RI; (401)-847-8000 x5597
smart mailer or arpanet: trr@rayssd.ray.com
old dumb mailer or uucp: {cbosgd,gatech,ihnp4,linus!raybed2} !rayssd!trr

hmm@laura.UUCP (Hans-Martin Mosner) (03/27/88)

Except for the last example, the behavior is correct.
In the last example, the deepCopy obviously copied only 2
levels instead of the whole structure.  Check the deepCopy methods,
in my opinion this is a bug, not a feature.

	Hans-Martin
-- 
Hans-Martin Mosner		| Don't tell Borland about Smalltalk - |
hmm@unido.{uucp,bitnet}		| they might invent Turbo Smalltalk !  |
------------------------------------------------------------------------
Disclaimer: Turbo Smalltalk may already be a trademark of Borland...

elt@entire.UUCP (Edward L. Taychert) (03/29/88)

In article <256@laura.UUCP>, hmm@laura.UUCP (Hans-Martin Mosner) writes:
> Except for the last example, the behavior is correct.
                 ^^^^ ^^^^^^^


This group's been empty on my machine for a while, I missed this discussion
and could use some help understanding...

I am writing an intelligent chess board in STV to help in some analysis; I set
the initial position of the board with a literal array. I was surprized
the second time I ran the program and, after initialization, got the
old board. Without understanding, I threw in a deep copy and it worked
fine. The documentation on deep copy is recursive "answer a deep
copy of the receiver". I've used lost of pointers and indirection in C;
if someone would help me and tell me what's going on, I will understand!
But I can't understand STV by its external behavior.

Thanks!


-- 

____________________________________________________________________________

Ed Taychert				Phone: USA (716) 381-7870
Entire Inc.				UUCP: rochester!rocksanne!entire!elt
435 E. Commercial Street
East Rochester, N.Y. 14445              A Xerox Company
_____________________________________________________________________________

hmm@laura.UUCP (Hans-Martin Mosner) (03/31/88)

In article <3160@entire.UUCP> elt@entire.UUCP (Edward L. Taychert) writes:
>I am writing an intelligent chess board in STV to help in some analysis; I set
>the initial position of the board with a literal array. I was surprized
>the second time I ran the program and, after initialization, got the
>old board.
This is normal.  *Literals* are not the same as *constants*.  They can be
modified like any other kind of object.

> Without understanding, I threw in a deep copy and it worked
>fine. The documentation on deep copy is recursive "answer a deep
>copy of the receiver".
Basically, it's up to each class how it defines deepCopy.  Characters,
for instance, don't produce a real copy but just return themselves.
The Object>>deepCopy method just creates a new object of the same class
and size as the receiver, and fills its instance variables with
deepCopies of its own instance variables.
The shallowCopy method on the contrary just creates the new object and
puts its own instance variables in it.
Naturally, deepCopy only works for non-circular structures.

>I've used lost of pointers and indirection in C;
>if someone would help me and tell me what's going on, I will understand!
Basically, each variable in Smalltalk just contains a pointer to an object,
not the object itself (with the notable exception of SmallInteger objects).
So in the shallowCopy just the pointers are copied, and the old objects
and its copy share the objects pointed to.

>But I can't understand STV by its external behavior.
Isn't this stuff documented somewhere ? (hint: go buy the blue book)

	Hans-Martin
>
>____________________________________________________________________________
>
>Ed Taychert				Phone: USA (716) 381-7870
>Entire Inc.				UUCP: rochester!rocksanne!entire!elt
>435 E. Commercial Street
>East Rochester, N.Y. 14445              A Xerox Company
>_____________________________________________________________________________
					   ^^^^^
Why use Smalltalk V ?  There's a Xerox Company which sells Real Smalltalk-80 :-)


-- 
Hans-Martin Mosner		| Don't tell Borland about Smalltalk - |
hmm@unido.{uucp,bitnet}		| they might invent Turbo Smalltalk !  |
------------------------------------------------------------------------
Disclaimer: Turbo Smalltalk may already be a trademark of Borland...

jans@tekcrl.TEK.COM (Jan Steinman) (04/01/88)

<I am writing an intelligent chess board in STV to help in some analysis; I set 
the initial position of the board with a literal array. I was surprized the 
second time I ran the program and, after initialization, got the old board.>

Literal arrays are not constants in Smalltalk, and assignment is by reference.  
Therefore:

	b _ a _ 'Crazy Jane'.
	a at: 8 put: $u.
	Transcript show: b.

will print 'Crazy June'.  If this is done in a workspace, the visible text does 
not change -- the literal that changes is int the DoIt compiled method.  If 
this code is placed in a method, a and b will indeed get initialized to 'Crazy 
June' the second time the method is executed.

Note that the source code of such a method, like the visible text in the 
workspace example, does not change, which can cause considerable confusion!

<I've used lost of pointers and indirection in C; if someone would help me and 
tell me what's going on, I will understand!>

The preceeding Smalltalk code is roughly the similar to the following C code:

	static char a[] = "Crazy Jane";
	char *b;
	b = a;
	a[7] = 'u';
	printf("%s", b);

:::::: Software Productivity Technologies    ---    Smalltalk   Project ::::::
:::::: Jan Steinman N7JDB	Box 500, MS 50-470	(w)503/627-5881 ::::::
:::::: jans@tekcrl.TEK.COM	Beaverton, OR 97077	(h)503/657-7703 ::::::