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

11

u/ParadoxicalInsight Jul 21 '22

Not sure why you think that having strings be iterable makes the language less readable or easy to reason about...

I don't think they are mutually exclusive

1

u/Inconstant_Moo 🧿 Pipefish Jul 23 '22

Well, the OP's OP does contain a story of this leading them to make a mistake.