The Generic for Protocol
Understand the three-value iterator protocol: iterator function, state, control variable.
The Generic for Protocol is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
How Generic for Works
The for statement calls an iterator function each iteration. It passes a state and a control variable, and stops when the function returns nil.
Three-Value Return
for var in iter, state, init do — Lua unpacks this as: iterator function iter, invariant state state, initial control variable init.
ipairs Protocol
ipairs(t) returns a stateless iterator function, the table as state, and 0 as initial control variable. Each call returns the next index and value.
for i, v in ipairs({10, 20, 30}) do
print(i, v)
endCustom Three-Value Iterator
Implement a custom stateless iterator by providing all three values explicitly.
local function iter(t, i)
i = i + 1
local v = t[i]
if v then return i, v end
end
for i, v in iter, {"a","b","c"}, 0 do
print(i, v)
endSimplifying with a Closure
A closure-based iterator only needs to return the iterator function (no state or control variable needed — they live in upvalues).
local function values(t)
local i = 0
return function()
i = i + 1
return t[i]
end
end
for v in values({1,2,3}) do print(v) endnil Terminates the Loop
The generic for loop exits as soon as the iterator function returns nil as the first return value. This is the termination contract.
Multiple Return Values
The iterator can return multiple values; each maps to a loop variable: for k, v in pairs(t).
for k, v in pairs({x=1, y=2}) do
print(k, v)
endState Parameter Usage
The state parameter is passed unchanged to every iterator call. For stateless iterators it holds all the context needed (e.g., the table itself).
Coroutine.wrap as Iterator
coroutine.wrap(fn) returns a function that resumes the coroutine each call; yielded values become return values — perfect as a generic for iterator.
local function countdown(n)
return coroutine.wrap(function()
for i = n, 1, -1 do coroutine.yield(i) end
end)
end
for v in countdown(5) do print(v) endIterator Composition
Iterators can be composed: wrap one iterator in another to add filtering or transformation.
local function mapped(iter, fn)
return function()
local v = iter()
if v ~= nil then return fn(v) end
end
endInfinite Iterators
An iterator never returning nil creates an infinite loop. Always provide a termination condition or break.
local function naturals()
local n = 0
return function() n = n+1; return n end
end
for v in naturals() do
if v > 5 then break end
print(v)
endGeneric for Question
When does the generic for loop stop?
Recap: Generic for Protocol
The generic for calls an iterator function with invariant state and control variable. It stops on nil. Closures simplify stateful iterators by encoding state in upvalues.
Frequently asked questions
Is the “The Generic for Protocol” lesson free?
Yes — the full text of “The Generic for Protocol” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Generic for Protocol”?
Understand the three-value iterator protocol: iterator function, state, control variable. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Generic for Protocol” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Generic for Protocol
- Stateless Iterators
- Stateful Iterators with Closures
- Coroutine-Based Generators