[Cuis-dev] String vs Symbol objects

Mark Volkmann r.mark.volkmann at gmail.com
Wed Jul 17 08:43:51 PDT 2024


Interesting! In this code I create two equivalent String objects, then
modify a character in one of them. I wondered if it would do a "copy on
write", but it did not. The modification affected both which was surprising
to me.

str1 := 'test'.
str2 := 'test'.
str1 at: 1 put: $b.

In this case, str1 and str2 refer to the same object in memory, so both now
have the value "best".

I suppose the lesson is that literal strings in the same scope might
(always?) use the same object in memory.

On Wed, Jul 17, 2024 at 10:12 AM Juan Vuletich <juan at cuis.st> wrote:

> On 7/17/2024 11:08 AM, Mark Volkmann via Cuis-dev wrote:
>
> When I enter the following lines in a Workspace, select them, and "Do it",
> I get an AssertionFailure error:
>
> str1 := 'test'.
> str2 := 'test'.
> self assert: str1 ~~ str2.
>
> Ken, are you saying that you do not get that error?
>
> --
> R. Mark Volkmann
> Object Computing, Inc.
>
>
> Side comment: an AssertionFailure is an Exception but not an Error (see
> class hierarchy).
>
> You get the AssertionFailure if you select all three lines and evaluate
> them together, but not if you evaluate them one by one.
>
> Truth is that str1 and str2 can not be assumed to be the same object. But
> they can not be assumed to be different object either. The compiler is free
> to reuse the string if it is easy enough.
>
> Cheers,
>
> --
> Juan Vuletichcuis.stgithub.com/jvuletichresearchgate.net/profile/Juan-Vuletichindependent.academia.edu/JuanVuletichpatents.justia.com/inventor/juan-manuel-vuletichlinkedin.com/in/juan-vuletich-75611b3twitter.com/JuanVuletich
>
>

-- 
R. Mark Volkmann
Object Computing, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240717/23e44fbc/attachment-0001.htm>


More information about the Cuis-dev mailing list