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
16
u/binarycow May 23 '26
Consider making a "view" type that isn't restricted to strings.
For example, C# has the following:
ArraySegment<T>- a "slice" of an array.Memory<T>- a "slice" of any contiguous heap dataReadOnlyMemory<T>- a read-only "slice" of any contiguous heap dataSpan<T>- a "slice" of any contiguous heap or stack dataReadOnlySpan<T>- a read-only "slice" of any contiguous heap or stack dataThe last one is interesting. In C#, a string literal isn't heap allocated. It's stored as raw data in the binary. So you can't access that as memory, but there's no reason you can't use treat it as a
ReadOnlySpan<char>!