-
Notifications
You must be signed in to change notification settings - Fork 371
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
for loops maintain the same block on iteration, which is referenced in any closures generated within #1135
Comments
I have a fix in #1585, it works like this: package main
func main() {
var fns []func()
for _, v := range []int{1, 2, 3} {
x := v*100 + v
fns = append(fns, func() { println(x) })
}
for _, fn := range fns {
fn()
}
}
// Output:
// 101
// 202
// 303 |
Closures should perform //Upvalue is a name for captured variables in Lua VM
type Upvalue struct {
val *PointerValue
} Example by @ltzmaxwell that currently doesn't work package main
func main() {
var fns []func() int
for i := 0; i < 5; i++ {
x := i
f := func() int {
return x
}
fns = append(fns, f)
x += 1
}
for _, fn := range fns {
println(fn())
}
}
// Output:
// 0
// 1
// 2
// 3
// 4 the outcome should be 1,2,3,4,5, while now get 0,1,2,3,4. |
@ltzmaxwell do we close #1818 or do you want to iterate on it? |
it's actually ready for review. thanks. |
title might be confusing, but here's the example:
(the
x :=
declaration is used to demonstrate that this does not referencev
, whereby the behaviour talked about here would actually be correct in the current go specification, though this is due to change in go1.22)go behaviour (expected):
gno behaviour:
The text was updated successfully, but these errors were encountered: