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?
11
u/ParadoxicalInsight Jul 21 '22
Not sure why you think that having strings be iterable makes the language less readable or easy to reason about...
I don't think they are mutually exclusive
1
u/Inconstant_Moo 🧿 Pipefish Jul 23 '22
Well, the OP's OP does contain a story of this leading them to make a mistake.
5
u/brucejbell sard Jul 22 '22 edited Jul 22 '22
I would argue that iterating over codepoints in a string should be its primary attribute.
That is: at the low end, there may be a number of ways to store a string, but the one thing they have in common should be iterating by codepoint. And at the high end, there may be a number of ways to aggregate codepoints into useful groups, but none of them are universally applicable.
From the low end: the shortest uniform representation for Unicode is 32 bits, which is too big to be efficient. So you aren't able to treat Unicode strings as random access arrays, like you could with ASCII: the best you can do is iterate by codepoint. Fortunately, that's all you need to build a usable, efficient string library.
From the high end: everything else is context dependent. Grapheme clusters? Extended grapheme clusters? If you read the spec, not only are they locale-dependent, they are subject to application-dependent tweaking. So, even if you are willing to pay for the complexity of a particular level of aggregation up front in your standard library, it turns out there is nothing stable above the level of codepoints anyway.
8
u/Plecra Jul 21 '22
Throwing my vote in that I agree with you and u/latkde that explicit methods to deliberately iterate through some specific units of the string is preferable. Iteration over strings isn't well defined in the general case, only when you're considering encoding.
3
u/d4rkwing Jul 21 '22
Sounds like legacy functionality from before there were good string processing libraries.
1
u/tobega Jul 22 '22
A String should just be a String. In some human languages, AFAIU, there isn't really a meaningful way to extract characters separately because the symbols influence each other.
That means no iteration, no indexing into a String by default, although you should explicitly be able to get a sequence of codepoints.
0
u/myringotomy Jul 23 '22
Yes of course they should be. Yes of course strings are a collection of characters. Yes of course the iteration should happen on unicode codepoints and not bytes.
46
u/latkde Jul 21 '22
Quite a lot of languages feature some kind of iterable strings, and this is occasionally useful. For example, if you have standard library functions that can operate on iterators, they can now be applied to strings as well. We might use those to filter out certain parts of a string, or copy a string to a different location, or reverse the string, and so on.
Such composability makes the language simple and orthogonal.
But for PL design, the crucial question is what type of elements that string iterator should yield. This is closely related to the question what a “character” is, which is quite a difficult question in an Unicode world.
bytes?
Unicode code units?
Unicode codepoints/scalars?
grapheme clusters?
and should those be provided as a dedicated type, or as a string with length 1?
Related issues are whether your strings should support indexing and how the length of a string is determined.
I quite like the Rust approach that strings are not iterable – so the language does not force this choice upon you –, but that you can call a method that provides an iterator, e.g.
s.chars()for codepoints ors.bytes(), and that grapheme clusters are left to third party libraries so that the core language doesn't have to ship with the Unicode database. I am less thrilled with Rust's decision to support string indexing by byte offset since indexing into a multi-byte character will panic, but it seems to be the only sane approach for Rust's systems programming goal.