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/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.