[Cuis-dev] Immutability package
Juan Vuletich
juan at jvuletich.org
Fri May 8 08:09:43 PDT 2020
On 5/8/2020 11:44 AM, ken.dickey--- via Cuis-dev wrote:
> On 2020-05-08 06:15, Juan Vuletich via Cuis-dev wrote:
>
> ... Something else that would be great is to gradually make Strings
> immutable. That would allow us to replace String with Rope if desired.
>
> Actually, Ropes are (or are supposed to be) immutable.
Of course. Just to be clear. My point is that making Strings immutable,
would help us remove all places where we mutate Strings. The
immutability package raises an exception, so it is easy to handle it by
actually mutating the String, but noting in which method this was called
for. In this way we can fix those methods so they don't modify the
String anymore, without breaking the system. When we are done with this,
we can switch to Ropes and nothing will break.
>
> Strings to Ropes is more of a style change.
>
> The main difference is that operations on ropes (e.g. #at:put:) return
> a _new_ rope, where Strings are mutated in place. So the first change
> would be ensuring all String mutators return a reference to the
> mutated String instance and all senders use the latest reference.
>
> This can make Undo easier. Just chain/push ref to previous ropes.
> Undo is pop last & redraw.
>
> If we made Text operations functional as well, TextEditor should have
> a simplified Undo.
>
> Different costs.
>
> Note that I did Ropes as preparation for using them in Unicode, where
> UniChars can be 1, 2, or 4 bytes. When inserting a Chinese character
> in an ASCII text string in Squeak, the Chinese character is a "wide"
> character, so Squeak changes the entire String to *all* wide
> characters, doubling its storage size. UniStrings are Ropes, so
> adding a Chinese UniChar just keeps a reference to the old Rope and
> remembers where the new char is for #at: and #at:put: . This memory
> is _much_ cheaper than doubling storage for the large strings
> typically used in text editing.
>
> Details in EnhancedText repository.
>
> Cheer,
> -KenD
Cheers,
--
Juan Vuletich
www.cuis-smalltalk.org
https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev
https://github.com/jvuletich
https://www.linkedin.com/in/juan-vuletich-75611b3
@JuanVuletich
More information about the Cuis-dev
mailing list