Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Go] Generate iterator for vectors #8444

Open
chriscraws opened this issue Dec 9, 2024 · 0 comments
Open

[Go] Generate iterator for vectors #8444

chriscraws opened this issue Dec 9, 2024 · 0 comments

Comments

@chriscraws
Copy link
Contributor

Go 1.23 introduced support for iterators. This allows code to define functions that supply values for Go's for range syntax.

Adding an optional flag for generating iterators would be a non-breaking change that allows for a more ergonomic way to visit all items in a vector.

Code such as

var weapon sample.Weapon
for i := 0; i < monster.WeaponsLength(); i++ {
     monster.Weapons(&weapon, i)
     ...
}

could be instead written as

for i, weapon := range monster.WeaponsIter() {
    ...
}

This would be accomplished via generated code that appears as follows (or similar):

func (rcv *Monster) WeaponsIter() iter.Seq2 {
    var weapon Weapon
    return func(yield func(i int, v *Weapon)) {
        for i := 0; i < rcv.WeaponsLength(); i++ {
            rcv.Weapon(i, &weapon)
            yield(i, &weapon)
        }
    }
}

I can author a PR if this feature would be acceptable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant