Register forum user name Search FAQ

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:

print (type ("nick"))  --> string


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:

42   -42.3   4.56e7   7.89e-10  0.3e12   22E+42


Numeric constants can also be written in hexadecimal like this:

0xA   0X0D   0x123456


* 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.

a = "Nick's cat"
b = 'Nick says "hello" to you'


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:

print("42" + 1)       --> 43
print("1e5" * "1e2")  --> 10000000


However strings always compare unequal to numbers:

print ("42" == 42)  --> false


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:


f = print    --  Assign the print function to the variable f
f ("hello")  --> hello


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:

my_table = { }


The table constructor can also add initial key/value pairs to the table, eg.

foods = {
   fish  = 10,
   chips = 11,
   cake  = 12
   }


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

assignment
break
comments
do
for (generic)
for (numeric)
function
identifiers
if / then / else
keywords
local
logical operators
precedence
relational operators
repeat
return
string literals
tables
while

Topics

Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua LPEG library
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua SQLite (database) interface
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting
Scripting callbacks - plugins

(Help topic: lua=data types)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.