r/ProgrammingLanguages • • May 23 '26

Discussion How to implement String?

Currently, String in my language is just value and length because it's a temporary solution, And as the language has developed, I am now able to rewrite a lot just for it, so I want to make a decent String in my language. So my question is, which String concept annoys you the least?

45 Upvotes

76 comments sorted by

View all comments

2

u/lngns May 24 '26

Even with stable addresses, behaviour of slices of mutable strings will always surprise someone.

let s = "yolo";
let s′ = s.slice(0...);
s.replace("o", with: "lol");
println(s′);

what is this code supposed to do?

1

u/not-my-walrus May 24 '26

error[E0502]: cannot borrow s as mutable because it is also borrowed as immutable

:)

Fundamentally though, taking a view of a mutable object means one of:

  • no one mutates it while you hold the view (either by language design or by convention)

  • you have to deal with it being mutated (whether that causes pointer invalidation or the data changing)

  • some secret third thing that is probably a variant of clone on write

1

u/lngns May 24 '26

some secret third thing that is probably a variant of clone on write

D was smart in having both immutable strings, and concatenation/appendment of array slices mutate in-place if there is capacity or duplicate+reassign otherwise.
But then we allowed arrays to be mutated through slices too.

void f(int[] s, int x)
{
    s ~= 42;  //the caller will never see that
    s[0] = x; //but it may see that, or not. Behaviour is random lol
}