data types |
---|
There are eight basic types in Lua: nil boolean number string table function userdata thread Note that values are typed, not variables. That is, a variable "foo" can contain any type of data, and that the type can change from moment to moment. You can find the type of a value by using the "type" function, for example:
The word returned by the "type" function will be one of the words in the list above. nil The value nil basically represents "no value". Global variables have the value nil by default, until something is assigned to them. boolean Boolean types only have two values, which are true and false. Boolean types are usually used where you simply want to record if something is true or not. However when doing condition tests (eg. an "if" statement) only two values are considered false: nil false Every other value (including zero, and an empty string) are considered true. number The number type holds numbers (internally as the "double" floating point type). There is no integer type, as the double type can hold integers without rounding errors up to 100,000,000,000,000. * In the case of MUSHclient, which is compiled with Microsoft Visual C++, the double type is documented to be in the range: 1.7E +/- 308 (15 digits) Numeric constants can be written with optional signs, decimal points and exponents like this:
Numeric constants can also be written in hexadecimal like this:
* See: http://en.wikipedia.org/wiki/Double_precision_floating-point_format The mantissa of "double" numbers is 53 bits, giving it the ability to store a precision of almost 16 digits. (53 * math.log10(2) == 15.95 digits). string Strings are any sequence of characters, including none. Strings are "8-bit clean" which means you can store any character in them including 0x00. Literal strings are delimited by single or double quotes. The same quote that starts the string ends it, allowing you to use the other quote inside the string, eg.
Copying strings is fast as there is only ever one copy of the same string. Strings cannot be modified "in place". To change a string you always make a new copy. If you attempt to do arithmetic on a string Lua will try to convert it to a number for you. For example:
However strings always compare unequal to numbers:
See the entry for "string literals" for more details about strings. function Functions are "code that can be executed". They can be either written in Lua (by you) or C (the internal libraries). They are "first-class" values, that is they can be stored in variables, passed as arguments to other functions, and can be returned as the return value of another function. For example:
See the entry for "function" for more details about functions. table Tables implement associative arrays. That is, arrays of key/value pairs. The keys and values can be any type, except nil. The only way to create a table is by using a "table constructor" like this:
The table constructor can also add initial key/value pairs to the table, eg.
In this example "fish" is a key, and the number 10 is the value associated with that key. See the entry for "tables" for more details about tables. userdata The userdata type lets Lua store "C" data into a Lua variable. This is mainly used by libraries, for example to hold information about open files, regular expressions and so on. thread Threads are used for co-routines, which are independent units of execution which can yield execution to the caller and resume later. See Also ... Lua keywords/topics
Topics
(Help topic: lua=data types) |
Enter a search string to find matching documentation.
Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.