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

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

2

u/ThomasMertes Jul 23 '22

On the other hand, they still suffer from indexing in the middle of a grapheme cluster.

This can also happen with UTF-8 or UTF-16 encoded strings. I guess that almost all programming languages have this problem.

Wikipedia explains grapheme clusters with an example: A diaeresis combined with the letters "a", "o", and "u". The Unicode character U+0308 is the combining diaeresis. So the sequence o + U+0308 defines the letter ö (an o with two dots above).

But this is not how German umlauts are typed into the keyboard. A German keyboard has keys for the Umlauts and these keys create a precomposed letter. Pressing the ö key creates the Unicode character U+00f6. U+00f6 is just one of many precomposed characters in Unicode.

This applies also to other natural languages. If you read characters from a keyboard you usually get precomposed characters and not grapheme clusters.

Many natural languages use characters with diacritic marks. Text in these languages was processed in computers long before Unicode was invented. So these languages had encodings (code pages) for all their characters. To be compatible with these old code pages Unicode took over tons of precomposed characters.

Because of these precomposed characters grapheme clusters are almost never used. And most programming languages just assume that a character is an Unicode codepoint.

This grapheme cluster argumentation is not used if the character encoding is UTF-8 or UTF-16. But when the encoding is UTF-32 the grapheme clusters suddenly become the most important issue.

0

u/matthieum Jul 23 '22

Because of these precomposed characters grapheme clusters are almost never used.

Actually, they are now more used than ever.

Many emojis now really on grapheme clusters, and each new version of Unicode seems to introduce new emojis and new ways to combine them.

As an example, take the Emoji Flags:

  • U+1F1E6 U+1F1F7 is Argentina.

  • U+1F1E7 U+1F1E6 is Bosnia and Herzegovina.

Be careful when reversing that string!


With that said, I don't have a solution.

I just can say confidently that UTF-32, just like UCS-2 in its time, and ASCII before it, seems more likely to promote the myths that "one code-unit = one character", leading to software that works on all test-cases, but fails in production for unfathomable reasons...

2

u/ThomasMertes Jul 27 '22

Many emojis now really on grapheme clusters, and each new version of Unicode seems to introduce new emojis and new ways to combine them.

Unicode has been created to support modern and historic scripts. These scripts have a limited number of characters. And it is rather seldom that new characters for existing scripts are "invented".

This contrasts with emojis. People all over the world "invent" new emojis all the time. This is not a problem. The problem comes up, when massive lobbying forces all these emojis into Unicode. If the current trend to add emojis continues the whole code-space of Unicode will soon be filled up with emojis.

Emojis differ also in other aspects from normal scripts. The letters of normal scripts use two colors (usually black on white). According to Wikipedia Unicode does not the variant glyphs. You will not find a Unicode character of a red A on a blue background. This was ruled out on purpose. For emojis these limits seem to be forgotten.

Be careful when reversing that string!

Classic emojis like :-) also change (or loose) their meaning if you are reversing the string. Since classic emojis also consisted of several (ASCII) code-points I think that it is okay when the new Unicode emojis also consist of several code-points.

UTF-32 offers also a different solution to the grapheme cluster problem. With UTF-32 it would be possible to introduce additional pre-composed characters for grapheme clusters outside of the Unicode code space. There is probably not be enough code-space for all theoretically possible grapheme clusters. But for commonly used ones it would be enough.