r/ProgrammingLanguages • u/funcieq • 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
View all comments
Show parent comment
1
u/WittyStick May 24 '26
The
sis passed by value, which meansstring_appendreceives a copy of the length. It can't mutate the length that the caller has - it can only return a newStringwhich has a new length.So the caller must make sure they abandon the old
spassed tostring_appendand use the new one it returns. This is only enforceable if you have substructural or uniqueness types.The alternative is to pass the
Stringby reference, where we can mutate the length - but then accessing the characters has the double-dereference I mentioned previously.