gno icon indicating copy to clipboard operation
gno copied to clipboard

for loops maintain the same block on iteration, which is referenced in any closures generated within

Open thehowl opened this issue 2 years ago • 6 comments

title might be confusing, but here's the example:

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()
        }
}

(the x := declaration is used to demonstrate that this does not reference v, 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):

101
202
303

gno behaviour:

303
303
303

thehowl avatar Sep 16 '23 14:09 thehowl

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

ltzmaxwell avatar Jan 26 '24 00:01 ltzmaxwell

Closures should perform late binding on captured variables. The values should be resolved when the closure is executed, not when it is created. For this to happen, we need to capture by *T and the value can be ad-hoc unwrapped so we keep the T semantics. A new type representing this should be created, which should looks something like

//Upvalue is a name for captured variables in Lua VM
type Upvalue struct {
   val *PointerValue
}

@jaekwon

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.

petar-dambovaliev avatar Mar 18 '24 11:03 petar-dambovaliev

Putting this here for reference. Here are the attempts to fix this that have not been merged: #1768, #1585, https://github.com/deelawn/gno/pull/3

This is the one currently in progress: https://github.com/gnolang/gno/pull/1780

deelawn avatar Apr 10 '24 02:04 deelawn

Putting this here for reference. Here are the attempts to fix this that have not been merged: #1768, #1585, deelawn#3

This is the one currently in progress: #1780

the latest attempt: #1818. #1780 has been proved not suitable for all scenarios.

ltzmaxwell avatar Apr 10 '24 05:04 ltzmaxwell

@ltzmaxwell do we close #1818 or do you want to iterate on it?

Kouteki avatar May 15 '24 08:05 Kouteki

@ltzmaxwell do we close #1818 or do you want to iterate on it?

it's actually ready for review. thanks.

ltzmaxwell avatar May 15 '24 09:05 ltzmaxwell

close this as solved by #2429. Go1.22 loopvar is not supported at the moment, defer for future optimization. cc: @moul @thehowl @jaekwon for visibility.

ltzmaxwell avatar Oct 22 '24 18:10 ltzmaxwell