Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!travelers.mail.cornell.edu!news.tc.cornell.edu!news.cac.psu.edu!newsserver.jvnc.net!newsserver2.jvnc.net!howland.reston.ans.net!ix.netcom.com!netcomsv!uu3news.netcom.com!netcomsv!uucp3.netcom.com!slcgate!servio!servio!aland
From: aland@servio.slc.com (Alan Darlington)
Subject: Re: Collections copy using VW2
Message-ID: <1995Jul11.195016.10417@slc.com>
Sender: news@slc.com (USENET News)
Nntp-Posting-Host: servio
Organization: GemStone Systems, Inc., Beaverton OR, USA
References: <3tp8sd$lt5@Tandem.CAM.ORG>
Date: Tue, 11 Jul 1995 19:50:16 GMT
Lines: 25

Sylvain Trudel <strudel@cam.org> writes:
> When I copy a collection, let's say an Array, and this array contains itself an 
> array, the copy of the second level array does not work. Instead it keeps the 
> reference to the original object.
> 
> a := #(1, #(1,2)).
> b:= a copy.
> 
> In this exemple the array #(1,2) is referenced by a and b.
> 
> Is it normal ? What should I modify to change this behavior ?

Yes, this is normal.  The #copy method defaults to #shallowCopy,
which only copies the receiver of the message.  For your own
classes, you may override #copy to do whatever you wish, but I
would not recommend changing #copy for system classes, as you
are pretty likely to break important things.

Many Smalltalks provide #deepCopy, which recursively copies all
components to all levels of nesting.  However, you must also be
cautious in using this, as circular structures get unfolded into
an infinite tree.  :-)

  Hope this helps.
  Alan
