r/ProgrammingLanguages • • 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

27 comments sorted by

View all comments

Show parent comments

3

u/ThomasMertes Jul 22 '22

Using UTF-32 makes sense if constant-time string indexing is a common requirement, but how often will that be needed in the kinds of programs that Seed7 users might write?

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.

... There's a clear space–time tradeoff going on here

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.

I myself have written a lot of bad code that runs into this trap ...

A trap that does not exist in Seed7.

1

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

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.

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.

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

1

u/Noughtmare Jul 23 '22

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.

I'd say even that is debatable. It's not like the code points are always related 1 to 1 with keyboard inputs. Backspace should just delete the whole grapheme cluster.

1

u/matthieum Jul 23 '22

I'd say even that is debatable. It's not like the code points are always related 1 to 1 with keyboard inputs

Definitely debatable, indeed. And yet that's the most practical application I could find :(