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:
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:
...then I can write a map function: ...but I cannot write a generic one that works with anything other than Foo. And if I need this: ...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.