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?

47 Upvotes

76 comments sorted by

View all comments

Show parent comment

1

u/WittyStick May 24 '26

Yeah, I wouldn't recommend doing this for mutable strings. But I was commenting on your previous post in which you said "pass the string by registers", which isn't pass-by-reference.

For immutable strings it's the most efficient - we can have multiple copies of the length since they're always going to be the same.

Mutable strings should be a separate type and should indeed by pass-by-ref. I prefer the approach of prefixing the length in the same allocation using the flexible member - as it avoids one alloc/free call, and is more cache optimal.

For concurrent mutable data structures (which you probably won't be doing with strings), then having the double-dereference is kind of necessary and you would pass-by-ref to a structure containing another pointer.