Function
Default Return
Lua function returns nil
as default.
lua
print((function() end)() == nil) -- true
1
Variadic Parameter
Variadic parameter in lua does not have name and default type, it's a special identifier ...
that has to be expanded to a table using { ... }
Such identifier can be used in:
{ ... }
: direct expansion to a table{ a, ..., b }
: append extra items around inside a tableselect('#', ...)
andselect(idx, ...)
to get length or pick one item from args without intermediate expansion
lua
local function foo(...)
local args = { ... }
print(table.concat(args, ', '))
end
foo('hello', 'world') -- hello, world
1
2
3
4
5
6
2
3
4
5
6
lua
Iterate over variadic without expansion
local function foo(...)
for idx = 1, select('#', ...) do
_ = select(idx, ...)
end
end
1
2
3
4
5
6
2
3
4
5
6
Multi-Returns
unpack(list: table, start?: integer, end?: integer)
: splatter items in successive indices from a tableselect(start: integer, ...any): ...unknown
: slice a splat from start into another splatselect(symbol: '#', ...any): ...unknown
: retrieve a length of the splat
{ <expr> }
: collect splats from<expr>
, a function call for example.lualocal function foo(...) return unpack { ... } end _ = { foo(1, 2, 3) } -- [1, 2, 3]
1
2
Iterator Function
Iterator in lua can be implemented by closure, which scopes local fields in its containing factory function, and operates iteration within the created iterator function.
- store state in outer scope
- return nil to cease the iteration
- consume by
for .. in
statement(the first return beingnil
would stop the loop)
lua
--- my ipairs impl at home
---@generic T
---@param array T[]
---@return fun(): integer, T
local function iter_array(array)
-- store state in outer scope
local idx = 0 -- [!code highlight]
local current = nil -- [!code highlight]
return function()
idx = idx + 1
if idx <= #array then
current = rawget(array, idx)
return idx, current
end
return nil, nil -- return nil to cease the iteration
end
end
for idx, item in iter_array { 1, 2, 3 } do
print(idx, item)
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23