Codectionary / Developer documentation / Lua

Lua closures

Lua reference for lua closures.

Syntax

local function make_counter()
  local count = 0
  return function() count = count + 1; return count end
end

Examples

Lua closures example

A focused example you can adapt while practising.

local function make_counter()
  local count = 0
  return function() count = count + 1; return count end
end

Best practices

  • Start with the smallest working example, then adapt it to your project.
  • Use the language’s standard library and idioms before introducing extra dependencies.

At a glance

Purpose
Lightweight scripting and embedding
File extension
.lua
Runs in
Lua interpreter or a host application
Usually used with
Host APIs and Lua modules

Specifications & further reading

Related Lua documentation