Closures
A closure is when you enclose a function inside of another, like so:
1
function a(var)
2
local x = 5
3
local function b()
4
print(x, var)
5
end
6
return b
7
end
Copied!
When you create a closure, all of the variables are accessible in its parent's scope. In this case, b has access to a's scope. This provides a way to access and manipulate variables inside a function after its parent returns. The variables that the closure can access are called upvalues. The variables x and var are both upvalues to function b in this case.
Take this example, a simple function to increment a value, which is inaccessible without the closure:
1
function counter()
2
local i = 0
3
return function()
4
i = i + 1
5
return i
6
end
7
end
Copied!
Here we define a function that returns an anonymous function (a function that has no name or identifier) which will increment the upvalue i and return the value back to us.
Each time we call counter, it generates a new counter for us, it can be called any amount of times and all the counters will have different values. Each time the returned function from counter is called (the function that increments), it will only change that counter, no others.
1
function counter()
2
local i = 0
3
return function()
4
i = i + 1
5
return i
6
end
7
end
8
9
local counter_1 = counter() -- now counter_1 holds a unique function to increment the upvalue
10
local counter_2 = counter() -- counter_2 holds a new unique incrementer
11
counter_1() -- increment our first counter, i is now 1
12
counter_2() counter_2() counter_2() -- increment counter_2 3 times, i is 3, counter_1 is unaffected
13
14
print(counter_1(), counter_2()) -- Output: 2 4
Copied!
For the print, the counters must be incremented again so we can get the value, since each time our incrementers run they return the variable after incrementing it.
Last modified 2yr ago
Copy link