<< Click to Display Table of Contents >> Navigation: Multi-Protocol MasterOPC Server > Lua 5.1 Reference Manual > The Language and syntax > Values and Types > Values and Types |
Lua is a dynamically typed language. This means that variables do not have types; only values do. That is, the language automatically defines a data type at the moment of initialization. There are no type definitions in the language. All values carry their own type.
All values in Lua are first-class values. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results.
There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table. Nil is the type of the value nil, whose main property is to be different from any other value; it usually represents the absence of a useful value. Boolean is the type of the values false and true. Both nil and false make a condition false; any other value makes it true. Number represents real (double-precision floating-point) numbers; a separate integer type does not exist. String represents arrays of characters. Lua is 8-bit clean: strings can contain any 8-bit character, including embedded zeros (’\0’) (see Lexical Conventions ).
Lua can call (and manipulate) functions written in Lua and functions written in C (see Function Calls ). In MasterOPC, such an ability is limited by several conditions. Therefore, we recommend to use the special Multi-Porotocol MasterOPC - User Protocol plugin in order to develop a complicated protocol in C.
The type userdata is provided to allow arbitrary C data to be stored in Lua variables. This type corresponds to a block of raw memory and has no predefined operations in Lua, except assignment and identity test. However, by using metatables, the programmer can define operations for userdata values. Userdata values cannot be created or modified in Lua, only through the C API. This guarantees the integrity of data owned by the host program.
The type thread represents independent threads of execution and it is used to implement coroutines. In MasterOPC, that functional is not used.
The type table implements associative arrays, that is, arrays that can be indexed not only with numbers, but with any value (except nil). That is, search in a table can be performed both by an index and by a value. Tables can be heterogeneous; that is, they can contain values of all types (except nil). Tables are the sole data structuring mechanism in Lua; they can be used to represent ordinary arrays, symbol tables, sets, records, graphs, trees, etc. To represent records, Lua uses the field name as an index. The language supports this representation by providing a.name as syntactic sugar for a["name"]. There are several convenient ways to create tables in Lua (see Table Constructors ).
Like indices, the value of a table field can be of any type (except nil). In particular, because functions are first-class values, table fields can contain functions. Thus tables can also carry methods (see Function Definitions ).
Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.
The library function type returns a string describing the type of a given value.