| a b |
a := b := 'foo'.
a == b. "=> true, so the identical string"
a at: 1 put: $b.
b "=> 'boo'
So in Squeak, and probably in Pharo, string literals are not immutable.
Other Smalltalks do support immutability, so perhaps string literals in those Smalltalks are indeed immutable.
Regarding copying, perhaps you're carrying assumptions over from other languages. 'foo'
is a literal in the sense that there's syntax for creating a String
, but really, 'foo'
is just another object. In Squeak and Pharo these literals are just normal String
instances, and so you are free to mutate them.
As the above snippet shows, that can lead to unexpected results, because if you change the first line to a := 'foo'. b := 'foo'.
you still end up with the same result: Squeak quietly shares that literal, rather than making a new one with the same value.