Skip to content

Language extensions

PxIgnis runs on PxLuaNova, a Lua 5.2 fork. This page documents extensions that aren’t part of standard Lua 5.2.

PxLuaNova adds a non-standard syntax for inline anonymous functions. Opt in per file by placing the magic comment on line 1:

--# nova syntax

This enables the \ + { two-character sequence to be recognized as a lambda literal. Without the pragma, \ outside a string literal is a syntax error.

\{ args -> body } — arguments followed by -> and the body.

I tried to implement implicit return like \{ 5 } that returns 5, but in Lua is too buggy. So just use return.

--# nova syntax
local double = \{ x -> return x * 2 }
print(double(21)) -- 42

Multi-arg:

--# nova syntax
local sum = \{ a, b, c -> return a + b + c }
print(sum(1, 2, 3)) -- 6

No parameters. Body is always a chunk; use return for values:

--# nova syntax
local answer = \{ return 40 + 2 }
print(answer()) -- 42

Body can contain statements:

--# nova syntax
local get = \{
print("side effects OK")
return 42
}
print(get()) -- 42

\{ ... } after a function call appends the lambda as the last argument. The body is always parsed as a chunk (no implicit return). This is especially useful with register():

--# nova syntax
register("greet") \{ ctx ->
ctx.player:send("hello!")
}

Works with any function, not just register():

--# nova syntax
local results = {}
local function each(t, fn)
for i, v in ipairs(t) do
fn(v, i)
end
end
each({ "a", "b", "c" }) \{ v, i ->
results[i] = v .. i
}
-- results == { "a1", "b2", "c3" }

Lambdas capture local variables from the enclosing scope, just like function:

--# nova syntax
function makeCounter()
local n = 0
return \{
n = n + 1
return n
}
end
local c = makeCounter()
print(c()) -- 1
print(c()) -- 2

The Lua standard library in PxLuaNova includes some Lua 5.3+ functions not present in standard Lua 5.2:

FunctionDescription
math.type(x)Returns "integer" or "float"
math.atan(y, x)Two-argument arctangent (Lua 5.3+)
math.min(...)Variadic minimum
math.max(...)Variadic maximum