3 ms·
I’ll cop to not having paid much attention to Go generics, but handwaving over my dubious syntax, what’s wrong with: ``` type Iter[T] interface { More
by binary132 3y ago
I’ll cop to not having paid much attention to Go generics, but handwaving over my dubious syntax, what’s wrong with:
```
type Iter[T] interface {
More() bool
It() T
}
type mapOver[T] struct { Iter[T]; func (T) T f }
func (m mapOver[T]) It() T {
return m.f(m.Iter.It())
}
func Map[T] (it Iter[T], f func(T) T) Iter[T] {
return mapOver{it, f}
}
type Range { to, at int }
func (r Range) More() bool {
return r.at < r.to
}
func (r Range) It() (is int) {
is = r.at;
r.at += 1
}
func DoEach[T] (it Iter[T], f func(T) T) {
for it.More() {
f(it.It())
}
}
// etc
iter.DoEach(iter.Range(10), fx.Stdout(fns.MultBy(2)))
// or whatever
```
Translating to English:
If you can do it just fine with the syntax you have, why do you need to add it to the core language?
- earthboundkid 3y agoThis was already proposed and rejected. See the prior discussions on GitHub. https://github.com/golang/go/discussions/54245 https://github.com/golang/go/discussions/54245
- binary132 3y agoNobody has to accept it. You can do it yourself today. That’s the point of my comment.