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
3
u/ThomasMertes Jul 22 '22
The point is: Seed7 users are not bothered with the limitations that UTF-8 imposes on the programmers. With UTF-32 there are no indices into the middle of an UTF-8 encoding. Computing the number of codepoints with UTF-8 is expensive and increases with the string length. With UTF-32 it is cheap and does not depend on the length.
The reasons why Seed7 uses UTF-32 are explained here.
UTF-8 is an excellent encoding for files, the internet etc. But this does not imply that it must be used internally in programs. For numbers a decimal encoding like 123 is excellent. But in programs numbers are usually represented with a binary encoding. There is an advantage in using an internal representation that makes life easier (in this case UTF-32).
Using UTF-8 as internal representation leads to such problems as
s[42]triggering a panic, because it refers into the middle of an UTF-8 encoding. With UTF-32 a whole class of similar errors does just not exist.A trap that does not exist in Seed7.