Lua - Closures



A closure is a function defined within a enclosing function scope. A closure function can access and modify the variables of enclosing function even after enclosing function finishes execution. This capability is termed as lexical or static scoping.

Key Concepts

Lexical Scoping

Lua uses lexical scoping which means scope of a variable is determined by the location of the variable where it is defined. When an inner function is defined within a function then inner function can access the local variables of the enclosing outer function.

Closures

When a function access the local variable of enclosing function, it closes over the variable. This is termed as Closure. A closure bundles the inner function functionality with the environment, the value of local variable of enclosing function.

Upvalues

When an enclosing function completes execution, its local variables are out of scope. But closure still have access to captured external variables of enclosing function and these values are termed as upvalues in Lua.

If multiple closures are created within a function and all closure refers to same local variable, the upvalue will be same for all closures. That effectively means that modification to upvalue in one closure reflects in another enclosure.

Example - Usage of Closure

In this example, we're creating a closure function to create a counter.

main.lua

function startCounter(initialValue)
   local count = initialValue
   -- a closure
   return function() 
      count = count + 1
      return count
   end
end

incrementer1 = startCounter(10)
incrementer2 = startCounter(100)

-- prints 11
print(incrementer1())
-- prints 12
print(incrementer1())
-- prints 101
print(incrementer2()) 
-- prints 102
print(incrementer2()) 

Output

When the above code is built and executed, it produces the following result −

11
12
101
102

Explanation

  • startCounter(initialValue) is an enclosing function accepting an initial value.

  • We've created a local variable count and assigned it the passed argument value.

  • An anonymous function is returned which forms a closure over count variable.

  • In closure function, we're incrementing the count variable and returning the same.

  • When incrementer1 = startCounter(10) is called, a closure is created with its own count initialized to 10.

  • When incrementer2 = startCounter(100) is called, another closure is created with its own count initialized to 100.

  • incrementer1 and incrementer2 holds references to closures. When these closures are called, the corresponding count variable is incremented, independent to each other.

Example - Usage of Closure to create a Iterator

In this example, we're creating an iterator to navigate elements of a list using closures.

main.lua

function listIterator(list)
   local i = 0
   local n = #list  -- size of the list
   -- closure function to get next element
   return function()
      i = i + 1
      if i <= n then return list[i] end
   end
end

list = {1, 2, 3, 4, 5}
for element in listIterator(list) do
   print(element)
end 

Output

When the above code is built and executed, it produces the following result −

1
2
3
4
5

Explanation

  • listIterator(list) is an enclosing function accepting a list.

  • We've created local variable i and n initialized with 0 and size of the list.

  • A closure is returned which increments the index by 1 and using a check on size of the list, the next value of the list is returned.

  • In generic for loop, we're using listIterator(list) to navigate through all elements and values are printed.

Advertisements