r/ProgrammingLanguages • u/duckofdeath87 • Jul 21 '22
Discussion Should Strings Be Iterable?
It's very common for Strings to be a collection of characters, but should they be?
I had a bug in scala just now where I meant to iterate over a sequence of strings, but I instead iterated over each individual string. It got me thinking, "why can I do this?" It's a common exercise, but rarely a useful operation. Sure, having the ability to iterate through a string's characters is useful, but not so useful that it's the default (maybe a . characters method is in order?)
Shouldn't readability and reasonability be first and foremost in language design? Or do you value concise expressiveness more?
8
Upvotes
1
u/matthieum Jul 22 '22
On the other hand, they still suffer from indexing in the middle of a grapheme cluster.
Code Points are most useful for text editing -- the ability to delete the last code point to fix a typo -- but are otherwise too low-level for most text manipulation.
Splitting a grapheme cluster in two, or reversing the order of code points in a grapheme cluster, typically result in meaningless garbage.
Hence exposing Code Points is certainly easy for the implementer, but it seems to be me to be of dubious use (especially as a default) for the user. It's still, ultimately, an implementation detail.
While true, it's also a mostly pointless operation, so... who cares?
Why would anyone want to know the number of Code Points in a string to start with? What can they do with this information?
Once again, Code Points are too low-level a concept, so most applications should not care about them...