[Cuis-dev] String vs Symbol objects

Juan Vuletich juan at cuis.st
Wed Jul 17 09:00:52 PDT 2024


On 7/17/2024 12:43 PM, Mark Volkmann via Cuis-dev wrote:
> 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.

The other lesson, at least to me, is that String mutation is bad (and 
ugly). It is generally better to create them anew, in a more functional 
style.

>
> On Wed, Jul 17, 2024 at 10:12 AM Juan Vuletich <juan at cuis.st 
> <mailto: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 Vuletich
>     cuis.st  <http://cuis.st>
>     github.com/jvuletich  <http://github.com/jvuletich>
>     researchgate.net/profile/Juan-Vuletich  <http://researchgate.net/profile/Juan-Vuletich>
>     independent.academia.edu/JuanVuletich  <http://independent.academia.edu/JuanVuletich>
>     patents.justia.com/inventor/juan-manuel-vuletich  <http://patents.justia.com/inventor/juan-manuel-vuletich>
>     linkedin.com/in/juan-vuletich-75611b3  <http://linkedin.com/in/juan-vuletich-75611b3>
>     twitter.com/JuanVuletich  <http://twitter.com/JuanVuletich>
>
>
>
> -- 
> R. Mark Volkmann
> Object Computing, Inc.


-- 
Juan Vuletich
cuis.st
github.com/jvuletich
researchgate.net/profile/Juan-Vuletich
independent.academia.edu/JuanVuletich
patents.justia.com/inventor/juan-manuel-vuletich
linkedin.com/in/juan-vuletich-75611b3
twitter.com/JuanVuletich

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240717/496bf5a3/attachment.htm>


More information about the Cuis-dev mailing list