function MakeCounter() local t = 0 return function() t = t + 1 return t end end local func = MakeCounter() for i = 0,10 do print(func()) end local func2 = MakeCounter() for i = 0,10 do print(func2()) end
>lua -e "io.stdout:setvbuf 'no'" "main.lua"
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
>Exit code: 0