The plan is, to introduce type-aliases in the `iter` package: type Yield[E any] = func(E) bool type Yield2[K, V any] = func(K, V) bool The reason to make these type-aliases instead of defined types is that it should be possible to define iterators without needing to import the `iter` package. But we still need to be assignable to `iter.Seq`. With defined types, that wouldn't be possible <https://go.dev/play/p/CGRXh8I2aMo>.
However, making them type-aliases, is blocked on #46477 <https://github.com/golang/go/issues/46477>. But it should happen for Go 1.124 On Wed, 21 Aug 2024 at 22:48, 'lijh8' via golang-nuts < golang-nuts@googlegroups.com> wrote: > Hi community, > > The type of yield occurs twice in the function declaration and > at the return statement. > > ``` > > func (s *Set[E]) All() iter.Seq[E] { > return func(yield func(E) bool) { > for v := range s.m { > if !yield(v) { > return > } > } > } > } > > ``` > > How about add yield types like this? > I think it can simplify the code a bit. > > ``` > > type Yield[V any] func(V) bool > type Yield2[K, V any] func(K, V) bool > > func sliceDemo[K int, V any](s []V) func(Yield2[K, V]) { > return func(yield Yield2[K, V]) { > for i, v := range s { > if !yield(K(i), v) { > break > } > } > } > } > > func mapDemo[K comparable, V any](s map[K]V) func(Yield2[K, V]) { > return func(yield Yield2[K, V]) { > for i, v := range s { > if !yield(i, v) { > break > } > } > } > } > > ``` > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/tencent_7983E02A0C5363ABC9B43CD458E0A1B7FC07%40qq.com > <https://groups.google.com/d/msgid/golang-nuts/tencent_7983E02A0C5363ABC9B43CD458E0A1B7FC07%40qq.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGf5ANEkQQgrgGiAt7tN0GapaOGJS3eSPupq0aNwkdmww%40mail.gmail.com.