One of the examples
in the VW documention has the line:
coll := 'This is a test' copy.
I looked at the
result of:
coll := 'This is a test'.
and both are
instances of ByteString.
What is the purpose/benefit of using 'copy'
?
Daniel
Klein
|
Daniel You don’t need to copy it if you are
not going to change it. Try performing a doIt on; string := ‘foobar’. Transcript show: string;cr. string at: 1 put: $T. Transcript show: string;cr. Notice that you get a modification
exception. That is because you are trying to modify a literal. Now change the
first line to; string := ‘foobar’ copy. and re-execute the doit. Terry From: Daniel Klein
[mailto:[hidden email]] One of the examples in the VW documention has the line: coll := 'This is a test' copy. I looked at the result of: coll := 'This is a test'. and both are instances of ByteString. What is the purpose/benefit of using 'copy' ? Daniel Klein |
In reply to this post by Daniel Klein-4
the copied version should be mutable, the uncopied version should be
immutable... try: coll := 'This is a test' copy. coll at: 5 put:$_. and try: coll := 'This is a test'. coll at: 5 put: $_. the first should work, the second should raise and error. :-) Kind Regards Karsten Daniel Klein wrote: > One of the examples in the VW documention has the line: > > coll := 'This is a test' copy. > > I looked at the result of: > > coll := 'This is a test'. > > and both are instances of ByteString. > > What is the purpose/benefit of using 'copy' ? > > Daniel Klein -- Karsten Kusche - Student - [hidden email] Georg Heeg eK - Köthen Handelsregister: Amtsgericht Dortmund A 12812 |
Thanks, that makes sense now in the context of the code example in the
documentation, which is: coll := 'This is a test' copy. readStrm := coll readStream. writeStrm := coll writeStream. [ readStrm atEnd ] whileFalse: [ | char | char := readStrm next. writeStrm nextPut: char asUppercase ]. ^coll The name 'copy' isn't very 'intention revealing' in this case. I would think that 'copyAsMutable' would have been a better name. Daniel Klein -----Original Message----- From: Karsten [mailto:[hidden email]] Sent: Monday, August 27, 2007 1:26 PM To: Daniel Klein; vwnc-list Subject: Re: #copy question the copied version should be mutable, the uncopied version should be immutable... try: coll := 'This is a test' copy. coll at: 5 put:$_. and try: coll := 'This is a test'. coll at: 5 put: $_. the first should work, the second should raise and error. :-) Kind Regards Karsten Daniel Klein wrote: > One of the examples in the VW documention has the line: > > coll := 'This is a test' copy. > > I looked at the result of: > > coll := 'This is a test'. > > and both are instances of ByteString. > > What is the purpose/benefit of using 'copy' ? > > Daniel Klein -- Karsten Kusche - Student - [hidden email] Georg Heeg eK - Köthen Handelsregister: Amtsgericht Dortmund A 12812 |
Daniel
#copy makes a copy of any object. It also makes the object mutable if it was immutable, unless #postcopy makes it immutable. Basically, you make a copy of an object when you want to preserve its state. Immutability is a recent addition to VW. It used to be that if you modified a literal then the next time the literal was used, i.e. the next time the message was received, it actually reflected the modification and not its original value. Terry =========================================================== Terry Raymond Crafted Smalltalk 80 Lazywood Ln. Tiverton, RI 02878 (401) 624-4517 [hidden email] <http://www.craftedsmalltalk.com> =========================================================== > -----Original Message----- > From: Daniel Klein [mailto:[hidden email]] > Sent: Tuesday, August 28, 2007 7:18 AM > To: 'vwnc-list' > Subject: RE: #copy question > > Thanks, that makes sense now in the context of the code example in the > documentation, which is: > > coll := 'This is a test' copy. > readStrm := coll readStream. > writeStrm := coll writeStream. > [ readStrm atEnd ] whileFalse: [ > | char | > char := readStrm next. > writeStrm nextPut: char asUppercase ]. > ^coll > > The name 'copy' isn't very 'intention revealing' in this case. I would > think > that 'copyAsMutable' would have been a better name. > > Daniel Klein > > > -----Original Message----- > From: Karsten [mailto:[hidden email]] > Sent: Monday, August 27, 2007 1:26 PM > To: Daniel Klein; vwnc-list > Subject: Re: #copy question > > the copied version should be mutable, the uncopied version should be > immutable... try: > > coll := 'This is a test' copy. > coll at: 5 put:$_. > > and try: > > coll := 'This is a test'. > coll at: 5 put: $_. > > the first should work, the second should raise and error. :-) > > Kind Regards > Karsten > > > > > Daniel Klein wrote: > > One of the examples in the VW documention has the line: > > > > coll := 'This is a test' copy. > > > > I looked at the result of: > > > > coll := 'This is a test'. > > > > and both are instances of ByteString. > > > > What is the purpose/benefit of using 'copy' ? > > > > Daniel Klein > > -- > Karsten Kusche - Student - [hidden email] > Georg Heeg eK - Köthen > Handelsregister: Amtsgericht Dortmund A 12812 |
Free forum by Nabble | Edit this page |