String manipulation failing in D6.02 Workspace

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

String manipulation failing in D6.02 Workspace

HBSteinberg
Hi,

I'm (once again) on my way through Ted Bracht's Smalltalk companion.

I'm struggling with this fairly easy statement:
   firstname := 'Bob'; at 1 put: $R

Evaluating this in D6.0.2 leads to an error dialog and the debugger
says in its error description 'Attempt to update read-only object'.

In contrast, when the same statement gets evaluated in a D5.1.4
workspace it just gets processed.

Can anyone give me a hint what has changed from D5 to D6 workspaces and
how deal with this?

Thanks in advance!

Regards
  Helmar.

--
Deutschland ist, wenn man trotzdem lacht.


Reply | Threaded
Open this post in threaded view
|

Re: String manipulation failing in D6.02 Workspace

Ian Bartholomew-21
HBSteinberg,

> Can anyone give me a hint what has changed from D5 to D6 workspaces and
> how deal with this?

 From the Dolphin 6 "What's new"

~~~~~~~~~~
We've now made all compiler generated literal objects immutable, and any
attempt to write to them will result in a Attempt to update read-only
object error.

In addition, you can also mark any other object as immutable by sending
it an #isImmutable: message with true as the argument. You might want to
do this for shared constants variables such as class variables. Should
you need to modify an immutable object then the Object>>whileMutableDo:
method can be used to temporarily disable immutability while a block is
  executed.

The immutability mechanism can also be used as a guard mechanism to
detect dirty objects since the VM now sends #errorInstVarAt:put: to an
immutable object if one attempts to assign to its named instance variables.
~~~~~~~~~~

So if you use a literal constant in a method (or workspace) you can't
alter it.  To work around it you could use the #whileMutableDo: method
mentioned above

firstname := 'Bob'.
firstname whileMutableDo: [firstname at: 1 put: $R].

or, possibly easier,  copy the literal into another (non literal) String....

firstname := (String withAll: 'Bob') "; not needed"
                at: 1 put: $R;
                yourself

Note the #yourself that would be needed (on the D5 version as well) to
get the code to work as expected.

--
Ian

Use the Reply-To address to contact me (limited validity).
Mail sent to the From address is ignored.


Reply | Threaded
Open this post in threaded view
|

Re: String manipulation failing in D6.02 Workspace

Udo Schneider
Ian Bartholomew wrote:
> or, possibly easier,  copy the literal into another (non literal)
> String....
>
> firstname := (String withAll: 'Bob') "; not needed"
>         at: 1 put: $R;
>         yourself
Or even simpler using #copy.

firstname := ('Bob' copy)
                        at: 1 put: $R;
                        yourself

CU,

Udo


Reply | Threaded
Open this post in threaded view
|

Re: String manipulation failing in D6.02 Workspace

Ian Bartholomew-21
Udo Schneider wrote:

>> or, possibly easier,  copy the literal into another (non literal)
>> String....

> Or even simpler using #copy.

Doh.  Even sillier seeing that I wrote "copy the literal" in the
preceding paragraph.

I think I had better go and lie down :-)

--
Ian

Use the Reply-To address to contact me (limited validity).
Mail sent to the From address is ignored.