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

Iterator#next should have return type restriction #10903

Open
straight-shoota opened this issue Jul 8, 2021 · 0 comments
Open

Iterator#next should have return type restriction #10903

straight-shoota opened this issue Jul 8, 2021 · 0 comments
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection

Comments

@straight-shoota
Copy link
Member

straight-shoota commented Jul 8, 2021

The module Iterator(T) revolves around the abstract def #next which is expected to return T or a stop indicator (Iterator::Stop). This expectation is currently not expressed in a type restriction. But it should be: Iterator(T)#next : T | Iterator::Stop.

Without this type restriction, an Iterator implementation can return any type in its #next method, which is prone to cause issues further down the line (see #10857 (comment) for an example). Adding this type restriction makes it easy to spot any incorrect implemtnations.

Unfortunately, this is currently not possible without introducing a major breaking change, because all abstract def implementations would be forced to specify a return type restriction as well. And most Iterator implementations don't do that.

#10904 proposes a compiler change as an alternative to introduce this return type restriction without a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection
Projects
None yet
Development

No branches or pull requests

1 participant