Values and types
Introduction
"Types" are meant to represent different kinds of information. For example, an answer to a question may be Yes or No (or true or false); but a person's name is unlikely be represented by any of those words. Similarly, types can also represent relationships between data. For example, it is obvious that we can add two numbers (e.g. 1+2=3)
. However, what happens if you try to add a number with your name? (e.g. 1+"John" = ????
). That is why types are useful and need to be understood.
There are eight types in the Lua programming language, but we will only cover six of them in this tutorial. These six types are:
Type | Example |
nil | ... we will talk about this later |
boolean | TRUE, FALSE |
number | 1, 2, 1.21, 3.141592 |
string | "hello", "john", "I do not have a cat" |
function | ... we will talk about this later |
table | { 1 = 2.1, 2= "john", 3 = false } |
Error due to type mismatch
As mentioned before, every variable in a Lua script has a type. When you try to perform math or other operations, these values need to 'make sense'.
Lets see the script below, in which we are trying to add a number to a string.
The execution returns
Even more, it is telling us that the error is in line 13.
Changing the type of a variable
Lua belongs to a family of programming languages called 'dynamically typed' languages. This means that the type of a variable can change during the execution of the language. Lets use this concept to fix the previous example.
And now, when you run it you get:
Which is what we were expecting... is not it?
Nil
The type nil can only hold a single value: 'nil', which represents abscence of data. This value is characterized by not being of any other value.
As you see when we run this script, the program prints the value 'nil' to the standard out. This is because v2 is a variable that has not been assigned any value (i.e. it basically does not exist).
Boolean
The type boolean can only contain two values: TRUE or FALSE. It is a pretty straightforward type, used for comparisons and other things.
Number
The type number contains any kind of real numbers, regardless if they are integers or not.
String
Strings represents 'chains of characters', known by most people as text
Function
Functions help writing code once and reusing it as many times as needed. However, we do not want functions to always do the exact same thing, but to change their behavior a bit. We can do that by providing arguments (i.e. inputs).
Even more, it is very likely that you want to use the results of a function for something else. In order to do this, functions can return values. See the example below.
Did you notice that we concatenated numbers with strings in the last line?
While the concept of function is present in most programming languages, not all of them consider them to be a type. This allows passing functions as arguments to other function, which can be confusing but useful.
Tables and Arrays
Tables are a very important type in Lua, and are the only way of structuring data. They are associative arrays than behave as dictionaries, objects or other types used in other programming languages.
Tables assign an implicit numeric key to all values provided without one, thus effectively behaving as Arrays.
Lets have a look at some examples.
Did you notice that the function 'greet' is in the table? We actually call it in the last line! To 'call' a function is to execute its code. This is done by using the '()' at the end.
Last updated