variables do not have types; only values do. All values carry their own type. Therefore, there are no type definitions in Lua.
width = 420
height = width*3/2 -- ensures 3/2 aspect ratio
color = "blue"
Figure 2: A very simple configuration file
More powerful configurations can be written using control flow and function definitions. Lua uses a traditional Pascal-like syntax, with reserved words and explicitly terminated blocks; semicolons are optional. Such syntax is familiar, robust, and easily parsed. A small example is presented in Figure 3. Notice that functions can return multiple values, and multiple assignments can be used to collect these values. Thus, parameter passing by reference, always a source of small semantic difficulties, can be discarded from the language.
function Bound (w, h)
if w < 20 then w = 20
elseif w > 500 then w = 500
local minH = w*3/2 -- local variable
if h < minH then h = minH end
return w, h
width, height = Bound(420, 500)
if monochrome then color = "black" else color = "blue" end
Figure 3: Configuration file using functions
Functions in Lua are first class values. A function definition creates a value of type function, and assigns this value to a global variable (Bound, in Figure 3). Like any other value, function values can be stored in variables, passed as arguments to other functions and returned as results. This feature greatly simplifies the implementation of object-oriented facilities, as will be seen later in this section.
Besides the basic types number (floats) and string, and the type function, Lua provides three other data types: nil, userdata, and table. Whenever explicit type checking is needed, the primitive function type may be used; it returns a string describing the type of its argument.
The type nil has a single value, also called nil, whose main property is to be different from any other value. Before the first assignment, the value of a variable is nil. Therefore, uninitialized variables, a major source of programming errors, do not exist in Lua. Using nil in a context where an actual value is needed (for instance, in an arithmetic expression) results in an execution error, alerting the programmer that the variable was not properly initialized.
The type userdata is provided to allow arbitrary host data, represented as void* C pointers, to be
stored in Lua variables. The only valid operations on values of this type are assignment and equality
Finally, the type table implements associative arrays, that is, arrays that can be indexed not only with integers, but with strings, reals, tables, and function values.
Associative arrays are a powerful language construct; many algorithms are simplified to the point of triviality because the required data structures and algorithms for searching them are implicitly provided by the language . Most typical data containers, like ordinary arrays, sets, bags, and symbol tables,