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

Iterators/Generators, next(), and unsafe yield statement types #58381

Open
1 task done
Renegade334 opened this issue Apr 30, 2024 · 0 comments
Open
1 task done

Iterators/Generators, next(), and unsafe yield statement types #58381

Renegade334 opened this issue Apr 30, 2024 · 0 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@Renegade334
Copy link
Contributor

Renegade334 commented Apr 30, 2024

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

🔎 Search Terms

iterator generator yield next

🕗 Version & Regression Information

Current behaviour has existed since 3.6.0.


With upcoming changes to iterators, I wonder whether it might be worth quickly revisiting the behaviour of the next() iterator method.

As discussed at #30790, when interacting with iterators manually, the compiler will always consider it valid if next() is called with zero-arity, even if TNext is of a type that cannot be void/undefined. The rationale for allowing this is that the iterator interface also has to be implemented by generators, where any parameter passed to the first call to next() gets thrown away, so it should be valid to allow the parameter to be absent.

This does have some type safety implications as originally discussed, but this also causes a related problem with the type of the yield statement in generators, which I don't think has ever been raised before:

function *g(): Generator<null, string, number> {
  const a = yield null; // const a: number
  return a.toString(16);
}

const it = g();
it.next();
it.next(); // TypeError: a is undefined

The function signature of next() allows the second call to be made without a parameter, but the compiler assumes that the type of the yield statement will always be TNext, and never be undefined. Although it violates the semantic intention of the generator signature, it seems like yield potentially returning undefined is a forced possibility if next() always allows calls with zero parameters, which was discussed previously and considered necessary.

Even if there's no alternative way to define next(), I wonder if the question of how to handle yield specifically needs considering. There was a mention in the design meeting minutes of considering new flags relating to iterators – maybe there's a case for one here?

related: #58243

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

3 participants