String literal at:put: question

Hans-Martin Mosner hm.mosner at cityweb.de
Mon Dec 25 11:03:49 UTC 2000


Avi Schwartz wrote:

> Hi,
>
> I am new to Smalltalk and Squeak, so please bear with me...

Welcome to The Club :-)

> I am trying to figure out why the result of
>
> 'abcdef' at: 1 put: $x
>
> is $x and not 'xbcdef'.

That has been the Smalltalk behavior since the dark ages... I presume
the intention is to make it semantically similar to assignment
expressions, whose value is equal to the assigned value. To achieve your
intention, you should evalutae
    'abcdef'
        at: 1 put: $x;
        yourself
which returns the receiver of the first message expression.

> Now, to complicate matters even worse for me, according to the book
> 'Smalltalk, Objects, and Design' by Chamond Liu, the result I should
> expect is an exception since a string literal is immutable.

Squeak does not currently have really immutable objects (with the
exception of SmallIntegers). This is also true for a number of other
Smalltalks. To make your code portable to Smalltalks with immutable
literals, you should write
    'abcdef' copy
        at: 1 put: $x;
        yourself
It would certainly be a great service to the Squeak community to
implement immutable objects, but I think that this requires quite some
in-depth knowledge of the VM and/or image to do, so this is certainly
not a beginner's project.

Cheers and happy holidays,
Hans-Martin






More information about the Squeak-dev mailing list