Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings **should** be considered as iterables #244

Closed
zloirock opened this issue Oct 21, 2022 · 4 comments · Fixed by #250
Closed

Strings **should** be considered as iterables #244

zloirock opened this issue Oct 21, 2022 · 4 comments · Fixed by #250

Comments

@zloirock
Copy link
Contributor

zloirock commented Oct 21, 2022

With #233, strings are not considered as iterables. It's complete nonsense.

for (const c of 'abc') console.log(c); // => 'a', 'b', 'c'

[...'abc']; // => ['a', 'b', 'c']

'abc'[Symbol.iterator](); // => StringIterator

// etc., many other cases, but only here:

Iterator.from('abc'); // => TypeError

WTF? Yes, making strings iterable was a controversial decision - but since they are already iterable and it's not possible to change it - maybe it's better to stop break the language by adding inconsistencies?

@zloirock
Copy link
Contributor Author

zloirock commented Oct 21, 2022

Furthermore,

Iterator.from(Object('abc')); // => StringIterator

But from ES2015 the behavior of non-modifying methods that can work with primitive wrappers, but can't work with primitives, was changed to accepting primitives, like:

Object.getPrototypeOf('abc'); // => String.prototype (from ES2015)

@michaelficarra
Copy link
Member

How do you think flatMap should handle iterable primitives? If it rejects them, it would be inconsistent with your proposed Iterator.from. If it iterates them, it is a potential footgun.

I think we've made the most practical choice here. And the committee usually sides with practicality over consistency. But I was already planning to bring up this decision during the presentation to committee: see item 3.b in this slide.

@bakkot
Copy link
Collaborator

bakkot commented Oct 24, 2022

I personally would prefer flatMap being inconsistent with Iterator.from over making Iterator.from reject strings.

@zloirock
Copy link
Contributor Author

zloirock commented Oct 25, 2022

I think we've made the most practical choice here.

I'm sure that it's not the most practical choice - inconsistency with the rest cases and breaking the iterables protocol is a more dangerous potential footgun.

I think that .flatMap should handle iterable primitives as iterables.

However, I'm OK if .flatMap will be inconsistent with .from and will reject strings - it's not so painful as Iterator.from case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants