-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: Go 2: range-over-function + range over types implementing iterator interface #65742
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
Comments
I'm pretty sure the same arguments have been refused for operator overloading. func (T) Add(T) T To overload the |
I find this exception rule quite undesirable, and it would be something that every developer must additionally be aware of. By the way, the naming convention for the method returning the sequence of all elements is All(), not Iterate(). |
I’m not sure I would consider these two comparable changes, but I understand the issue you perceive |
I don’t love the exception myself, but it’s necessary to preserve backward compatibility. I don’t really care if it’s Iterate or All. I chose Iterate because it fits well into Go’s single method interface naming conventions. An interface named Aller feels gross compared to Iterator |
this appears to just be a rehash of #54245 ? |
To be specific, #54245 ran into the problem that it wasn't backwards compatible. If you have |
Go Programming Experience
Intermediate
Other Languages Experience
Python, C#, Javascript, Java
Related Idea
Has this idea, or one like it, been proposed before?
I don't believe so, but it feels likely
Does this affect error handling?
no
Is this about generics?
not really
Proposal
I think many of us Gophers can agree, the experimental range-over-func features, along with the iter package are seriously exciting changes! As I have tested these changes out with the release of Go 1.22 I have found implementation incredibly easy and very much inline with Go's historically readable design. However, I have also found the use of iterator functions somewhat obtuse in some scenarios.
As an example, let's use an ordered map as that's one of the example motivations for this change:
As you can see, I have to call
m.Iterate()
to actually use the iterator. That's not a huge deal, but the built-inmap[K]V
has no such limitation. Imo in an ideal world, interacting with my own types should be as simple and clean as interacting with built-in types. Hence this proposal. I propose the following:iter
package:Iterator.Iterate()
method.This change would allow for my previous
OrderedMap
for loop to be written:While at first glances this may appear to be a trivial reward for the work, I believe this change could make iterators feel incredibly polished, in addition to decluttering code. I admit that when looking at a
for ... range
loop that iterates over aniter.Iterator
, it is less clear what is happening. However, with IDEs and intellisense what they are today, I believe this would be a very small lost in clarity of code.Note: the only restrictions to this change would be that any type that is already iterable (e.g. slices, arrays, maps, etc...) only ever use their typical behavior on iteration, even if they implement the
Iterator
interface.Language Spec Changes
The compiler, static type checker, probably some others
Informal Change
The purpose of this change is to simplify iterating over custom types by allowing any type to implement the
Iterator
interface, allowing any type to very easily be iterated over in afor ... range
loop. It even allows overriding default iteration behavior on a type, such as ontype MyType []string
by allowing that type to implement theIterator
interface.Is this change backward compatible?
I believe so.
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
It would make it harder, but only marginally so. I think you would have to see this one time, and you would understand it.
Cost Description
No response
Changes to Go ToolChain
No response
Performance Costs
Pretty much no difference
Prototype
No response
The text was updated successfully, but these errors were encountered: