4 ms·
Since we're talking about implementing it in usercode, as opposed to adding it to the language, we fortunately have the option to rework it (or have a couple co
by Kamq 3y ago
Since we're talking about implementing it in usercode, as opposed to adding it to the language, we fortunately have the option to rework it (or have a couple completing implementations to see what works in practice).
If you're looking for those particular features, I'd recommend adopting go's `, ok` idiom that appears in several places in the language. A type like:
type Iterator[T any] interface {
Next() (T, bool)
}
Lets you write code like this, if that's more to your liking:
for current, ok := i.Next(); ok; current, ok = i.Next() {
// do things here
}
Which, really seems like a more go-like way to do this in the language as well (ranging over a function then becomes syntactic sugar for the above).