string sharing (possible bug?)
bfelton at ibm.net
Wed Dec 9 15:44:52 UTC 1998
It does seem a bit peculiar, doesn't it? But it all
boils down to how literals are handled. The source of
your difficulty is that Squeak treats 'hello' as a string
literal with 2 occurences in your code fragment, thus both
a and b point to the same object.
You derive the correct, or nearly correct, Smalltalk
programming principle from your results. (BTW, this result
leads to such other oddities as IBM's VisualAge treating
literals as read-only objects. This works, of course, but
leads to other peculiar behavior, such as forbidding your
Note that the problem is not so much constants as it is literals;
for safety's sake, you might want to consider sending #copy to
literal strings -- this works in all Smalltalks, including VA
(oddly, a copy of a literal is not that literal, but a new
object, and mutable).
At 10:28 AM 12/9/98 -0500, you wrote:
>Hi, I'm pretty new to Smalltalk, so this might just be something I
>don't know about. I'm using Squeak 2.2 on Linux.
>If I execute the following in the workspace,
> |a b|
> a := 'hello'.
> b := 'hello'.
> a at: 2 put: $i.
>I find that b has been changed to 'hilo', even though I've modified
>a. The same behaviour occurs if I use constant arrays, i.e. every
>object that I assign #(1 2 3) to points to the same object, and if I
>modify one of them, the others change too.
>I realize that in Smalltalk variables *point* to objects. But in the
>above code, I assigned the constant string 'hello' to "a" and "b"
>separately -- I didn't assign a to b -- yet they still obviously point
>to the same object. Is this what is supposed to happen, or is this
>some copy-on-write string sharing scheme gone awry? If this is the
>correct behaviour, then I suppose a good Smalltalk programming rule
>would be, "don't modify an array or string object that has been
>initialized from a constant", because otherwise you may inadvertantly
>affect some other part of your program which also has a variable
>initialized from an identical constant. But that just doesn't seem
>right to me. I know that symbols are all unique, but I didn't know
>that string and array constants are also unique.
More information about the Squeak-dev