3 ms·
Adding a standard iterator interface to Go?
- bediger4000 4y agoI have reservations about this. Go's range works as it does to provide some level of thread safety safety. C++ experience leads me to fear that ineptly written iterators will make for-loops look simple but fail in puzzling manner due to race conditions. Not every language has to include everything from Java and C++
- assbuttbuttass 4y agoI think the idea of a standard iterator interface is good, it will allow standard implementations of Map, Filter, and other convenient functional helpers. But I have mixed feelings on the overloading of the "for x := range" syntax. One of the things I like about go is that it's easy to dive into a new code base and understand what's going on. Overloading the range operator means to understand a for loop I need to jump around to understand the iterator implementation. It's already pretty easy to loop over an iterator, e.g. for { x, ok := it.Next() if !ok { break } ... } I'm not convinced that overloading range is worth it. But on the other hand, I'm sure I would get used to it fairly quickly
- amscanne 4y agoIt’s actually even simpler, just a one-liner: for x, ok := it.Next(); ok; x, ok = it.Next() { // … }