iterator_item icon indicating copy to clipboard operation
iterator_item copied to clipboard

Proposed alternate syntax: -> impl Iterator<Item = ReturnType>

Open joshtriplett opened this issue 3 years ago • 3 comments

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).

joshtriplett avatar Nov 11 '21 00:11 joshtriplett

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.

estebank avatar Nov 11 '21 02:11 estebank

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 avatar Nov 11 '21 18:11 dignifiedquire

@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.

joshtriplett avatar Nov 11 '21 23:11 joshtriplett