News for hackers. Stuff that matters.
Re:Why Go and Not Rust? by atombender on Monday September 16, 2019 @03:43PM (#20987837)
That's an apples-to-oranges comparison because your iterator interface here isn't generic like Rust's. It isn't reusable.

Your iterator has to be specific to a set of types — "v.Item" in your example is always of some specific type. Using an untyped interface{} would be worse than any alternative because every single use would have to use type switches and casting; you lose out of compile-type type safety.

Because of this, even if you have a local iteration system, it isn't composable beyond your package and its types. Given this (simpler) interface:

  type Iterator interface {
    Next() (Foo, error)
  }
...then I can write a map function:

  func Map(it Iterator, mapper func(Foo) Foo)) Iterator
...but I cannot write a generic one that works with anything other than Foo. And if I need this:

  func MapFooToBar(it Iterator, mapper func(Foo) Bar)) Iterator
...then I have to write it just for that purpose.

That's why there's no "iterator library" for Go, like there is with Rust.

There are no comments on this comment yet.