-
Notifications
You must be signed in to change notification settings - Fork 34
should Iterator.from
and AsyncIterator.from
check callability of "next" method eagerly?
#228
Comments
Specifically, they currently do this check eagerly when you pass an iterator, but not when you pass an iterable. Probably best to do the check in both cases. |
I think it makes sense to check. This is sort of an interesting timing case in the current use of [] = { [Symbol.iterator]() { return {} } }; // does not throw vs [a] = { [Symbol.iterator]() { return {} } }; // throws |
I guess the equivalent in this proposal would be |
I don't see any benefit to trying to achieve a perceived symmetry with empty array destructuring. |
The goal isn't to achieve symmetry with empty array destructuring. I think it just has the same user expectations. We probably intentionally designed destructuring to not check that the RHS produced a valid iterator since it won't be consumed. I can see the same rationale applying to iterator helpers that end up consuming nothing. Symmetry with empty destructuring is just a nice outcome. |
I really don't think so. Why would we have? Rather, there's no callability check in GetIterator because normally it's redundant - you'll just be immediately calling it, so there's no need to check explicitly - and there's no reason to ever write If the spec was intentionally omitting checks on the RHS when the LHS was empty, it would avoid reading But with Iterator.from it will be common to get an iterator without immediately invoking |
I agree about |
Sorry for the confusion, I mentioned the empty destructuring as sort of a way of showing how strange the "not checking" behavior is in our current spec. I definitely think we should check, and not try to match what destructuring does 😄 |
Why is take(0) even an option? |
We'd have to special-case that to not do the check, because that check is normally done eagerly. I really do not think we should add a special case that to avoid giving an error when calling |
And in fact right now validation of the receiver is done before coercing the arguments, and "validation of the receiver" includes checking that its |
@bakkot It wouldn't be a special check, we would just remove the check from |
Ah, ok. I think that would be much worse; we should do validation up front. |
I'm actually fairly neutral at the moment. I like early failures but I also like not failing when there's no need to. If I had to pick, I guess I lean toward the latter since delayed failure feels more consistent with the delayed computation in the rest of the iterator helpers. |
I think "consistency with delayed computation" is much less important than (indeed, is subsumed by) "is this going to be a better or worse experience to program with", and I think delaying this validation is a much worse experience to program with. |
Is it not the case that not causing a failure is a better experience than causing one unforced? |
I think within a program using iterators, the size of it is probably the variable part (as opposed to what apis you're using on it), so I would think of that more as hiding errors that are likely to pop up later. |
Not when the underlying program is in fact buggy, no. It's only a better experience if the user actually expected that it would not fail. It will be much more common to run into this by passing the wrong data but also coincidentally not consuming the iterator for other reasons, in which case the underlying bug is masked. And that is definitely a worse experience. |
I doubt this was intentional. Would it be out of scope for this proposal to introduce the callability check right inside |
@bergus open an issue on 262 and we can do a needs consensus PR. Unfortunately it's probably too short notice to do for the upcoming meeting, so it will have to wait until December. |
I know it may be too late, but I just found that I missed this issue.
I understand the current behavior (lazy throw) is to keep |
As proposed by @rbuckton in #117 (comment).
The text was updated successfully, but these errors were encountered: