Lua Types Tutorial |
|
Please look at TutorialExamples for notes about running the examples here. We'll use the print() function to print out values or calculations on those values. The parentheses around the arguments are important and will cause an error if missed out.
> print(2) -- print the number two. 2 > print("hello") -- print the string hello. hello
Lua allows simple arithmetic on numbers using the usual operators to add, subtract, multiply and divide.
> print(2+2) 4 > print(2-7) -5 > print(7*8) 56 > print(7/8) 0.875
= operator.
> x = 7
> print(x)
7
x is created when the number 7 is assigned to it. We use the print() function again to print out the value of x. We can now use the value in x for other calculations.
> x = x * 9 > print(x) 63 > print(x*2) -- will not change the value of x 126 > print(x) 63
print(x*2) does not change the value of x because it was not assigned using the = but x = x * 9 multiplies the current value of x by 9 and stores the new value in x again.
For more information on Lua's number type you can look at the NumbersTutorial.
Lua also uses strings (i.e. text) types:
> print("hello") hello
> who = "Lua user" > print(who) Lua user
.. operator between two strings.
> print("hello ") hello > print("hello " .. who) -- the variable "who" was assigned above hello Lua user > print(who) Lua user
.. operator does not change the value of message unless the = assignment operator is used, just like numbers.
> message = "hello " .. who > print(message) hello Lua user
+ operator to concatenate strings. i.e.:
> message = "hello " + who stdin:1: attempt to perform arithmetic on a string value stack traceback: stdin:1: in main chunk [C]: ?
For more information on Lua's string type you can look at the StringsTutorial.
Boolean values have either the value true or false. If a value is not true, it must be false and vice versa. The not operator can be placed before a boolean value to invert it. i.e. not true is equal to false.
> x = true > print(x) true > print(not x) false > print(not false) true
==, and not equals ~= operators will return boolean values depending on the values supplied to them.
> print(1 == 0) -- test whether two numbers are equal false > print(1 == 1) true > print(1 ~= 0) -- test whether two numbers are not equal true > print(true ~= false) -- is true not equal to false? true
For more information on Lua's Boolean type you can look at the ExpressionsTutorial.
Lua has a general-purpose aggregate datatype called a table. Aggregate data types are used for storing collections (such as lists, sets, arrays, and associative arrays) containing other objects (including numbers, strings, or even other aggregates). Lua is a unique language in that tables are used for representing most all other aggregate types.
Tables are created using a pair of curly brackets {} . Let's create an empty table:
> x = {}
> print(x)
table: 0035C910
When we display a table value using the built in print function Lua just displays the fact that it is a table and unique identifier for that table (i.e. its address in memory). We can print out the contents of a table but that comes in the TablesTutorial.
We can construct tables containing other objects, such as the numbers and strings described above, e.g.
> x = { value = 123, text = "hello" }
> print(x.value)
123
> print(x.text)
hello
> y = { const={ name="Pi", value=3.1415927 }, const2={ name="light speed", value=3e8 } }
> print(y.const.name)
Pi
> print(y.const2.value)
300000000
For more information on Lua's table type you can look at the TablesTutorial.
In Lua, functions are assigned to variables, just like numbers and strings. Functions are created using the function keyword. Here we create a simple function which will print a friendly message.
> function foo() print("hello") end -- declare the function > foo() -- call the function hello > print(foo) -- get the value of the variable "foo" function: 0035D6E8
foo and it displays (like tables) that the value is a function, and has unique identifier for that particular function. So, being a value just like any other, we should be able to assign functions to variables, just like the other values, and we can.
> x = function() print("hello") end > x() hello > print(x) function: 0035EA20
For more information on Lua's function type you can look at the FunctionsTutorial.
nil is a special value which indicates no value. If a variable has the value nil then it has no value assigned to it and therefore will no longer exist (or doesn't exist yet). By setting a variable to nil you can delete a variable. e.g.
> x = 2.5 > print(x) 2.5 > x = nil > print(x) nil
nil.
> print(x == nil) true > x = 7 > print(x == nil) false > print(x) 7
Userdata values are objects foreign to Lua, such as objects implemented in C. These typically come about when an object in a C library is exposed to Lua. An example of a userdata value is a file handle. A userdata often behaves like a table, and you can largely ignore the difference unless you are implementing one. Userdata is a more advanced topic discussed further in the [Lua Reference Manual].
A thread value represents an independent (cooperative) thread of execution. These are discussed further in CoroutinesTutorial.
You might have noticed that whilst we created the above variables, we did not have to specify which type of variable we were creating. For example,
a = 1 b = "hello" c = { item1="abc" }
a = 1 a = "hello" a = { item1="abc" }
As Lua is a reflective language, we can use the Lua function type() to get a description of the type of a particular object.
> x = "123" -- a string > print(x, type(x)) -- show the value of x and its type 123 string > x = x + 7 -- add a number to the string which forces coercion > print(x, type(x)) -- again show the value and type 130 number