Files
Marc VertesandGitHub 761df6ee59 interp: correctly init variables assigned from function call
In the case of a Go short definition (i.e. `a, b := f()`), the new defined variables must be (re-)created in order to preserve the previous value (if in a loop) which can be still in use in the context of a closure. This must not apply to redeclared variables which simply see their value reassigned.

The problem was both occuring in callBin() for runtime functions and assignFromCall() for functions created in the interpreter.

Fixes #1497.
2023-03-24 11:46:05 +01:00

37 lines
478 B
Go

package main
import (
"fmt"
"strconv"
)
type monkey struct {
test func() int
}
func main() {
input := []string{"1", "2", "3"}
var monkeys []*monkey
for _, v := range input {
kong := monkey{}
divisor, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
fmt.Print(divisor, " ")
kong.test = func() int {
return divisor
}
monkeys = append(monkeys, &kong)
}
for _, mk := range monkeys {
fmt.Print(mk.test(), " ")
}
}
// Output:
// 1 2 3 1 2 3