News for hackers. Stuff that matters.
Re:Iterators are (maybe) coming to Go by Groxx on Saturday August 12, 2023 @12:14AM (#37096985)
I'm guessing this is following the same / highly similar motivation as rsc's coroutines post about a month ago: https://research.swtch.com/coro

I welcome people embracing function closures more, as I feel they're a drastically under-used language tool (particularly in Go where everyone crams everything into single-func interfaces which are almost exclusively far worse). More exposure to it helps people become comfortable with it, and now that we have generics it's finally not a pile of pain and agony at all times. And just using functions makes it very flexible / doesn't repeat the "X for me but not for thee" issues of past Go versions.

I'm not entirely sure how I feel about this proposal though. It kinda feels like it's trying to put a round peg into a square hole. If you try hard enough, everything can go in the square hole! But that doesn't mean it's a good fit.

Re:Iterators are (maybe) coming to Go by anonymoushn on Saturday August 12, 2023 @01:43AM (#37097367)
It would be nice if the proposal defined the semantics of getting single item from the iterator (or detecting that it is out of items) and exposed an api for this, then defined the behavior of range as equivalent to some pattern using these primitives.

As is the proposal says "there should be some coro.Pull type thing" which is a bit terse and says nothing about whether the implementation of coro.Pull will be fast or whether it will be built on range + channels

Re:Iterators are (maybe) coming to Go by znkr on Saturday August 12, 2023 @03:11AM (#37097816)
Re:Iterators are (maybe) coming to Go by anonymoushn on Saturday August 12, 2023 @05:07AM (#37098368)
This does not contain anything about the proposed implementation of Pull, of course.
Re:Iterators are (maybe) coming to Go by Groxx on Saturday August 12, 2023 @04:32PM (#37103983)
Wouldn't this be the implementation?

    func Pull[V any](push func(yield func(V) bool)) (pull func() (V, bool), stop func()) {
        ... // snipped because it's big
    }
Re:Iterators are (maybe) coming to Go by anonymoushn on Saturday August 12, 2023 @11:43PM (#37106481)
That is an implementation in terms of primitives that are not in the proposal.
Re:Iterators are (maybe) coming to Go by Groxx on Sunday August 13, 2023 @11:57AM (#37111103)
I don't believe so - the post is pretty much all about how you can do this right now in normal Go code. And e.g. a bit after that is a link to the "full code" which is runnable: https://go.dev/play/p/hniFxnbXTgH

Possibly you're interpreting yield as a new keyword? It's just an argument to Pull.

The only not-(currently-)available-in-Go stuff that I can recall from the post is the performance optimization at the very end.

Re:Iterators are (maybe) coming to Go by anonymoushn on Sunday August 13, 2023 @12:44PM (#37111578)
You can read the proposal here: https://github.com/golang/go/issues/61405. It is linked in the first paragraph of TFA.

Yes, I know you can build a 100x-too-slow version of this on top of channels, but that's not the proposed extension to the language.

Re:Iterators are (maybe) coming to Go by Groxx on Saturday August 12, 2023 @03:34AM (#37097945)
I suspect getting a single item out will be that you just make a

  func next[T](i iterfunc[T]) T {
    var item T
    i(func(it T) bool){
      item = it
      return true // likely
    })
    return item
  }
and use it. Semantics should be the same as ranging would desugar to (since that's all this is), though the variable arity will be slightly annoying of course.
Re:Iterators are (maybe) coming to Go by anonymoushn on Saturday August 12, 2023 @05:08AM (#37098377)
Sure, and that should be somewhere in the very long posts about this or in the very long github issue.
Re:Iterators are (maybe) coming to Go by assbuttbuttass on Saturday August 12, 2023 @10:14AM (#37100412)
I don't think it's so easy. An iterfunc doesn't store any state explicitly, so each time you call i it restarts from the beginning
Re:Iterators are (maybe) coming to Go by Groxx on Saturday August 12, 2023 @02:54PM (#37103126)
Aah, yes, that's an excellent point. Thanks! That does complicate things a lot more.

Quite a lot more, as it essentially means you can only consume from iterators you create... which sorta defeats the purpose of iterators as a value, and degrades them almost completely to just a `range` helper.

Re:Iterators are (maybe) coming to Go by throwaway073123 on Sunday August 13, 2023 @12:07PM (#37111194)
> particularly in Go where everyone crams everything into single-func interfaces which are almost exclusively far worse

Depending on scope, I've grown to prefer the single func interface.

For a smaller scope use, say filepath.Walk, absolutely, pass in a closure, good stuff.

For larger stuff, like say a chain of http middleware and handlers, I think it can be very limiting.

you get a big benefit from using the interface over a function: the underlying type _can_ have more methods and implement.

this gives room to implement a parallel interface, for example, to collect all possible routes, or to trace what handlers a request would pass through.

I don't think there's a one-size-fits all approach, use whatever gives the effort:utility tradeoff you're looking for.

Re:Iterators are (maybe) coming to Go by Groxx on Sunday August 13, 2023 @12:45PM (#37111582)
Func references can have methods as well fwiw :) They just need to have a named type.

But sure, if you want to build a multi-method thing (or upcast to detect optional methods), an interface is the natural choice - it's no longer a single func.

Re:Iterators are (maybe) coming to Go by throwaway073123 on Monday August 14, 2023 @03:24PM (#37125621)
Absolutely. If I see a one method interface, I generally assume that there's a ThingDoerFunc type that implements the interface.

Likewise, (ThingDoerImplemetation{}).DoThing is a valid func reference

Hence I say there's not really a one size fits all, there are a options for different circumstances.