Modifying strings in place ( VW 7.5)

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

Modifying strings in place ( VW 7.5)

Ivo Ardaliev
Hi all,
I've got a strange problem here.
The folowing code works in Squeak and Smalltalk Express , but not in VW:

string := 'now is the time'.
string at: 1 put: $N.
string.
 
(btw, this is from the book "Smalltalk by Example")
I get a "no modification error".
I looked at the code for at:put: and I found out , that  'now is the time ' is actually something like a literal for the system , because it is immutable. I tried " 'asdf' isImmutable " in WorkSpace and it returns true.
So, my question is, how do I do this in VW?
I thought of converting  the string  to ByteArray or some Collection, modify it , and then convert it back to ByteString but this is too complicated. There must be an easier way to do it. I know I'm missing something.


Reply | Threaded
Open this post in threaded view
|

Re: Modifying strings in place ( VW 7.5)

Vassili Bykov-2
Change the first line to

string := 'now is the time' copy.

Unlike the original literal, the copy is mutable.

On Nov 25, 2007 6:03 PM, Ivo Ardaliev <[hidden email]> wrote:
> Hi all,
> I've got a strange problem here.
> The folowing code works in Squeak and Smalltalk Express , but not in VW:
>
> string := 'now is the time'.
> string at: 1 put: $N.
> string.
[...]

Reply | Threaded
Open this post in threaded view
|

Re: Modifying strings in place ( VW 7.5)

Joseph Bacanskas-4
In reply to this post by Ivo Ardaliev
Hi Ivo:

Try:

| string |
string := 'now is the time' copy.
string at: 1 put: $N.
string.


On Nov 25, 2007, at 6:03 PM, Ivo Ardaliev wrote:

> Hi all,
> I've got a strange problem here.
> The folowing code works in Squeak and Smalltalk Express , but not in  
> VW:
>
> string := 'now is the time'.
> string at: 1 put: $N.
> string.
>
> (btw, this is from the book "Smalltalk by Example")
> I get a "no modification error".
> I looked at the code for at:put: and I found out , that  'now is the  
> time ' is actually something like a literal for the system , because  
> it is immutable. I tried " 'asdf' isImmutable " in WorkSpace and it  
> returns true.
> So, my question is, how do I do this in VW?
> I thought of converting  the string  to ByteArray or some  
> Collection, modify it , and then convert it back to ByteString but  
> this is too complicated. There must be an easier way to do it. I  
> know I'm missing something.
>
>

Thanks!!
Joseph Bacanskas [|]
--- I use Smalltalk.  My amp goes to eleven.

Reply | Threaded
Open this post in threaded view
|

Re: Modifying strings in place ( VW 7.5)

Andre Schnoor
In reply to this post by Ivo Ardaliev
Ivo Ardaliev wrote:

> Hi all,
> I've got a strange problem here.
> The folowing code works in Squeak and Smalltalk Express , but not in VW:
>
> string := 'now is the time'.
> string at: 1 put: $N.
> string.
>  
> (btw, this is from the book "Smalltalk by Example")
> I get a "no modification error".

Sure.  Your example above attempts to modify the compiled method itself
(i.e. the literal string associated with it). As others have pointed
out, making a copy of the compiled "resource" is the right thing to do.

Without immutability, snippets like your example would accidentally
modify code behind the scenes. Self-destructing code is a bad thing in
itself,  but in addition to that, the source code would not reflect the
changes, further adding to the confusion :-)

Andre

Reply | Threaded
Open this post in threaded view
|

Re: Modifying strings in place ( VW 7.5)

Dave Stevenson-2
In reply to this post by Ivo Ardaliev
If you have a real need, you can explicitly make something immutable
like so:
        'foo' , 'bar' isImmutable: true

But it is not recommended to arbitrarily make things mutable, especially
literals, because the compiled method that holds it will no longer match
its source. That's why no one has yet mentioned #isImmutable: to you,
but instead suggested that you use #copy. So don't use #isImmutable:
unless you really understand the consequences and really need it. But if
you're learning about VW's immutability scheme you should at least know
about it, if for no other reason than to know what not to do.

Dave

Ivo Ardaliev wrote:

> Hi all,
> I've got a strange problem here.
> The folowing code works in Squeak and Smalltalk Express , but not in VW:
>
> string := 'now is the time'.
> string at: 1 put: $N.
> string.
>  
> (btw, this is from the book "Smalltalk by Example")
> I get a "no modification error".
> I looked at the code for at:put: and I found out , that  'now is the
> time ' is actually something like a literal for the system , because it
> is immutable. I tried " 'asdf' isImmutable " in WorkSpace and it returns
> true.
> So, my question is, how do I do this in VW?
> I thought of converting  the string  to ByteArray or some Collection,
> modify it , and then convert it back to ByteString but this is too
> complicated. There must be an easier way to do it. I know I'm missing
> something.
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Modifying strings in place ( VW 7.5)

Cesar Rabak
In reply to this post by Andre Schnoor
Andre Schnoor escreveu:

> Ivo Ardaliev wrote:
>> Hi all,
>> I've got a strange problem here.
>> The folowing code works in Squeak and Smalltalk Express , but not in VW:
>>
>> string := 'now is the time'.
>> string at: 1 put: $N.
>> string.
>>  
>> (btw, this is from the book "Smalltalk by Example")
>> I get a "no modification error".
>
> Sure.  Your example above attempts to modify the compiled method itself
> (i.e. the literal string associated with it). As others have pointed
> out, making a copy of the compiled "resource" is the right thing to do.
>

Not so sure!

It attempts to modify a string which in other Smalltalk dialects is
acceptable...

> Without immutability, snippets like your example would accidentally
> modify code behind the scenes. Self-destructing code is a bad thing in
> itself,  but in addition to that, the source code would not reflect the
> changes, further adding to the confusion :-)

This is a safety net decided and implemented in VW Smalltalk. Make sense
we consider it 'natural' (this is VW list), but is not the only way it
is implemented.

My 0.019999...


--
Cesar Rabak
GNU/Linux User 52247.
Get counted: http://counter.li.org/