On this page

Scripting

Values, variables and statements

Values and types

nil, booleans, integers (64-bit, checked), numbers (64-bit floating point), strings (immutable Unicode text), tables and functions. There is no userdata type and there are no coroutines; metatables are limited (Tables).

  • Integers and numbers. Integer literals stay integers, and arithmetic between integers stays whole except for / and ^, which always give a number. Integer overflow is an error; it never wraps round. // rounds down (towards negative infinity), % takes the sign of the divisor, and whole-number // or % by zero is an error. Floating-point division by zero gives infinity (1/0 == math.huge).
  • Strings are Unicode text. #s, string.len, string.sub and string.byte count UTF-8 bytes, and an operation that would split a character in the middle is an error rather than producing broken text. .. joins strings and turns integers and numbers into text ("n=" .. 42 gives "n=42"); any other type is an error. Escapes \128 to \255 aren't allowed in string literals, because strings are always valid text.
  • Truthiness: nil and false are false; everything else, including 0 and "", is true. and and or return one of their operands and stop as soon as the answer is known.
  • Equality works across the two numeric forms (1 == 1.0), and values of different types are simply unequal, never an error. Ordering (<, <=) needs two numbers or two strings (compared byte by byte); anything else is an error, even nil <= nil.
  • No automatic conversion in arithmetic: "42" + 0 is an error. Convert with tonumber (The standard library).
LUA
7 // 2                    --> 3
-7 // 2                   --> -4
-7 % 3                    --> 2
7 / 2                     --> 3.5
1 == 1.0                  --> true
1 == "1"                  --> false
"n=" .. 42                --> "n=42"
0 and "yes" or "no"       --> "yes"
math.maxinteger + 1       --> error: overflow
(tonumber("42") or 0) + 8  --> 50

Variables and scope

LUA
local x = 1                  -- a local, visible to the end of its block
local a, b = 1, 2            -- several at once
x = 5                        -- assignment
x  --> 5

Scope follows position, exactly as in Lua: in local a = a + 1 the right-hand side sees the outer a. Reading a global that doesn't exist is an error (unknownGlobal); Lua would quietly give nil, but SwiftLua treats it as the bug it usually is. In the default strict mode, creating a global is reported before the script runs, so declare locals instead:

LUA
g = 10                       -- the checker reports this: declare it with local

Statements

Form Notes
if … then … elseif … else … end as in Lua
while cond do … end as in Lua
repeat … until cond the body runs at least once; the condition can see the body's locals
for i = a, b [, step] do … end counting loop; both bounds included. A step of 0 is an error and the bounds must be numbers. If any of start, limit or step is a floating-point number, the loop variable is one on every pass; it is an integer only when all three are integers
for k, v in ipairs(t) do … end the array part, in order (see Tables)
for k, v in pairs(t) do … end the whole table, in a defined order (see Tables)
break leaves the innermost loop; outside a loop it is an error
do … end an explicit block, for scope
return … see Functions and multiple returns: several values are allowed, up to a limit
local function f(…) … end a local function that can call itself
function f(…) … end a global function

There is no goto, no continue, and no compound assignment such as +=.

The loop variable of a counting for is a fresh copy on each pass, so assigning to it inside the body doesn't change how many times the loop runs.

function t.m() end and function a.b:m() end are shorthand for assigning a field; the : form adds a hidden first parameter called self. Dotted paths can be as long as you need. Both are exactly the same as t.m = function(…) … end.

LUA
local tries = 0
repeat
  local done = tries >= 2
  tries = tries + 1
until done                   -- the condition sees the body's local
tries  --> 3

local passes = 0
for i = 1, 3 do i = 100; passes = passes + 1 end
passes  --> 3

local out = {}
for i = 1, 3.0 do out[#out + 1] = tostring(i) end   -- a float bound gives float values
string.join(" ", out)  --> "1.0 2.0 3.0"

local counter = { n = 0 }
function counter:bump(by) self.n = self.n + by end
counter:bump(5)
counter.n  --> 5