#copy question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

#copy question

Daniel Klein-4
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
Reply | Threaded
Open this post in threaded view
|

RE: #copy question

Terry Raymond

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

===========================================================
Terry Raymond
Crafted Smalltalk
80 Lazywood Ln.
Tiverton, RI  02878
(401) 624-4517      [hidden email]
<http://www.craftedsmalltalk.com>
===========================================================


From: Daniel Klein [mailto:[hidden email]]
Sent: Monday, August 27, 2007 1:10 PM
To: [hidden email]
Subject: #copy question

 

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

Reply | Threaded
Open this post in threaded view
|

Re: #copy question

Karsten Kusche
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

Reply | Threaded
Open this post in threaded view
|

RE: #copy question

Daniel Klein-4
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


Reply | Threaded
Open this post in threaded view
|

RE: #copy question

Terry Raymond
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