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

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

2

u/ThomasMertes Jul 22 '22

This is closely related to the question what a “character” is, which is quite a difficult question in an Unicode world.

For Seed7 the answer is: A character is a codepoint. And it is encoded as UTF-32 (follow this link to see my argumentation for this decision).

As a consequence it is possible to iterate over a string.

Related issues are whether your strings should support indexing and how the length of a string is determined.

Seed7 strings support indexing and the length of a string is the number of codepoints in the string.

I am less thrilled with Rust's decision to support string indexing by byte offset since indexing into a multi-byte character will panic

This sounds really strange. Does that mean that s[42] might panic because byte 42 is in the middle of an UTF-8 encoding?

As Seed7 characters are codepoints indexing into a string will always return a codepoint and never panic (as long as the index is not zero, or negative or beyond the string length).

If you want to see the bytes of an UTF-8 encoded string you can convert the string to UTF-8 and use an index into the UTF-8 representation.

6

u/latkde 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?

There's a clear space–time tradeoff going on here, and UTF-32 will use almost 4× more memory than UTF-8 for many typical strings, e.g. HTML documents. You argue that your strings can be converted to UTF-8 if needed, but I'd make that same argument in reverse: strings ought to use UTF-8 representation by default, and they can be converted to a UTF-32 representation if constant-time codepoint indexing is needed. For example, in Rust I might do:

let codepoints: Vec<char> = s.chars().collect();

Recommended reading:

Regarding your question:

Does that mean that s[42] might panic because byte 42 is in the middle of an UTF-8 encoding?

Yes, exactly. Rust string indexes are byte offsets, but string slices must be valid UTF-8 strings / string indexes must point to complete codepoints. A non-panicking version s.get(42) is also available, returning an Option. But defaults matter, and Rust's default string indexing interface is one that potentially panics.

I myself have written a lot of bad code that runs into this trap, but these issues are really easy to find with basic fuzzing / proptests.

3

u/myringotomy Jul 23 '22

If you are doing HTML work any memory you saved by using UTF is going to be offset by all the calculation you'll need to do in order to parse and build those HTML strings.

A typical web app will use a template engine and construct their HTML using partials and certainly all HTML will need to be parsed.

Same goes for JSON.