gnolang / gno

Gno: An interpreted, stack-based Go virtual machine to build succinct and composable apps + Gno.land: a blockchain for timeless code and fair open-source
https://gno.land/
Other
842 stars 343 forks source link

Missing return value error in Poll tutorial #2310

Closed Molaryy closed 2 weeks ago

Molaryy commented 3 weeks ago

Description

I was testing the how to write a simple dApp with @agherasie and we found out that the code given for the package doesn't work when trying to deploy it with gnokey.

This is the error that we got: Screenshot 2024-06-08 at 18 04 14

It happens at line 67 in the function VoteCount:

func (p Poll) VoteCount() (int, int) {
    var yay int

    p.Voters().Iterate("", "", func(key string, value interface{}) bool {
        vote := value.(bool)
        if vote == true {
            yay = yay + 1
        }
    })
    return yay, p.Voters().Size() - yay
}

we solved this problem by adding a return false at the end of Iterate:

p.Voters().Iterate("", "", func(key string, value interface{}) bool {
        vote := value.(bool)
        if vote == true {
            yay = yay + 1
        }
        return false
    })

Like the tree package says when the callback returns true, the iteration is stopped, so by adding false we will reach every vote.

leohhhn commented 2 weeks ago

Thanks for noticing this - if you want, you can make a PR to fix it in the docs :)

agherasie commented 2 weeks ago

Thanks for noticing this - if you want, you can make a PR to fix it in the docs :)

Opened https://github.com/gnolang/gno/pull/2356 to solve this !