iterator_item
iterator_item copied to clipboard
Proposed alternate syntax: -> impl Iterator<Item = ReturnType>
I very much like the proposed yield syntax, and I think we should use that syntax.
However, I don't want to hide the iterator type behind yields. I'd like to show the iterator type, with something like -> impl Iterator<Item = ReturnType> (or analogously for AsyncIterator).
I acknowledge that this is more verbose. However, I think it'll make the behavior and function type more obvious, and I consider it analogous to -> Result<T, E> (which we don't hide).
This seems like a reasonable compromise, also giving an affordance to the user to also assert a specific kind of Iterator being returned (like DoubleEndedIterator). I was trying to hide it a little bit because I was thinking it would allow us to make changes here to the returned type, for example if we ever make a new expression to consume generators and give back a non-unit value through resume, but making the returned type explicit such a change would be opt-in, which is reasonable.
When comparing with the async version, I would expect the Iterator part not to be shown, given that there is like for async fn an additional marker on the fn part:
async fn foo() -> i32 { ... }
// vs
fn foo() -> impl Future<Output = i32> { ... }
// and iterators
fn* foo() -> i32 { ... }
// vs
fn foo() -> impl Iterator<Item = i32> { ... }
Edit: This does unfortunately not solve the issue of the potential change of return types @estebank mentioned though.
@dignifiedquire
When comparing with the async version,
I don't want to reopen that particular discussion here, but: some folks, myself included, believe that should have been spelled -> impl Future<Output=T> (or perhaps -> impl Future<T>) as well. And I think that may be possible to improve upon.
@estebank Exactly.