Lua Data Types
Lua - Data Types
Section titled “Lua - Data Types”Lua is a dynamically typed language. This means variables themselves don’t have fixed types; instead, types are associated with the values they hold. Values can be stored in variables, passed as arguments to functions, and returned as results.
Lua has eight fundamental data types for values:
| Value Type | Description |
|---|---|
| nil | Represents the absence of a useful value. It has only one possible value: nil. Variables not explicitly initialized default to nil. In conditional contexts, nil evaluates to false. |
| boolean | Includes the values true and false. Used for conditional logic. Note: In Lua, only false and nil are considered falsy; all other values (including 0 and empty strings) are truthy. |
| number | Represents numeric values. Prior to Lua 5.3, all numbers were double-precision floating-point numbers. Lua 5.3 introduced an integer subtype, but type() still returns “number” for both integers and floats. Lua handles conversions automatically where appropriate. |
| string | Represents immutable sequences of characters. Strings can be enclosed in single quotes ('...'), double quotes ("..."), or long brackets ([[...]] for multi-line strings). |
| function | Represents functions. Functions are first-class citizens in Lua, meaning they can be stored in variables, passed as arguments, and returned as results. They can be written in Lua or C. |
| userdata | Represents arbitrary C data. Allows C code to make custom data types available to Lua. There are two types: full userdata (Lua manages its memory) and light userdata (just a C pointer). |
| thread | Represents independent threads of execution, used to implement coroutines. Coroutines allow for cooperative multitasking, not preemptive OS-level threads. |
| table | The sole data structuring mechanism in Lua. Tables are associative arrays (also known as maps or dictionaries) that can be used to represent ordinary arrays, sets, records, graphs, trees, objects, and more. They can hold any value except nil as keys or values (using nil as a key or assigning nil to a key effectively removes the key-value pair). |
The type() Function
Section titled “The type() Function”Lua provides a built-in function type() that returns a string describing the type of a given value. Here are some examples:
print(type("What is my type?")) --> stringprint(type(10)) --> numberprint(type(3.14)) --> numberprint(type(true)) --> booleanprint(type(print)) --> function (print is a global function)print(type(type)) --> function (type is also a function)print(type(nil)) --> nil
local myTable = {}print(type(myTable)) --> table
local co = coroutine.create(function() end)print(type(co)) --> thread
-- For an undefined variable, its value is nilprint(type(undefinedVariable)) --> nil (assuming undefinedVariable has not been assigned a value)
-- The type of the result of type(X) is always stringprint(type(type(123))) --> string (because type(123) is "number", and type("number") is "string")Output of the above code:
stringnumbernumberbooleanfunctionfunctionniltablethreadnilstringRemember that variables not explicitly initialized default to nil. A common pitfall for beginners is assuming 0 or an empty string "" might be false in conditions; in Lua, they are both considered true. Only false and nil evaluate to false in boolean contexts. Understanding these types is crucial for effective Lua programming.