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.
-- type_error.lua
-- =============
-- Tell Emp that we do not intend to solve anything
auto_solve = false
-- Create two variables
v1 = 1 -- v1 has type number
v2 = "Hello" -- v2 has type string
-- Lets try to add these, and we get an error
-- because v2 is a string and v1 is a number.
print(v1 + v2) 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.
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.
Last updated