Hello Nim friends!
i dont known how i can solve this problem. See minimal and runnable example:
type
Foo = object
something: proc()
var
foos: seq[Foo]
# test 1 does not work because of closures
# for i in 1..10:
# var f: Foo
# f.something = proc() = echo $i
# foos.add(f)
# test 2 also does not work and i dont know why.
for i in 1..10:
let j = i
var f: Foo
f.something = proc() = echo $j
foos.add(f)
for f in foos:
f.something()
in Javascript and in C# i can simply copy the variable (like in test2), but i miss something. I use the current dev version of Nim 1.7.1 on Windows 10.
Can you help me?
Thank you very much! I didnt know about this.
For other readers this is the solution:
for i in 1..10:
closureScope:
let j = i
var f: Foo
f.something = proc() = echo $j
foos.add(f)