Lua simple types

By Samir Tine, published on December 2021

I hope you have enjoyed reading the first part of this tutorial to learn programming with Luart. Let's dive into the second part !

Lua can manipulate different types of values

Do you remember when I made an analogy between function arguments and your wallet ? When you have to buy something you can use a lot of payment methods. Humans don't exist to have money (well, I hope so !), but I must admit that it plays an important role in relationships. What do computers do if humans handle money? Only data. And you will be manipulating data constantly when coding.

In the first part, you have already manipulated data values without even realizing it:

  • A text value: "Listen!"
  • Functions are values too in Lua : beep() and print()
  • A value that represent nothing : nil
Even the sys aisle in the runtime library is a value, but that will be covered later.

In fact money can be represented by different values : cash, credit card, coupons, check... and so on. Even if they all represent money, each one of this values have specific properties. Some are writable (checks), some need an authentication code (credit cards)... Lua values have the same kind of different properties. It's now time to discover the different values that we will manipulate when coding with Lua.

Lua data types

Lua can manipulate eight kind of values, called types :

Simple types :

  • nil
  • boolean
  • number
  • string

Complex types :

  • table
  • function
  • userdata
  • thread

Another last complex type is specific to Luart and is covered in a tutorial for more experimented users : object.

This tutorial will now focus on the simple types, the complex types will be covered in the next tutorial.

The nil type

It is certainly the easiest type to understand, but it is also the most challenging to imagine. A nil value represents...nothing. But wait, if the value doesn't represent anything, then it can't exist! That's it, the value nil indicates that there is nothing. I find this obscure, even philosophical, concept brilliant:

Imagine you are seated at the table. On your plate, you see an apple, which is not surprising since you're terribly hungry. Therefore, there is a fruit-type on your plate. But what happens to the value once you've eaten the apple? It goes to nil. Incredible isn't it ?! In fact, the value nil refers to non-existence or nothing.

You can imagine that with nil you can't do much else except make values disappear. More on that later.

At this time, let's run the print() function with nil as an argument instead of the sentence "Listen !" from myprogram.lua to see what it will look like :

print(nil) Sys.beep()

Run the program and see the message printed to the screen, followed by the bip :


Perhaps you are wondering why the print() function prints something when there is no value. However, the subtlety is in the fact that nil means absolutely nothing, but it's still a value! Thus, the word nil appears on the screen.

The boolean type

Boolean values only represent logical values, either true or false, and nothing else. These values are very useful when checking if a piece of information is true or false, for example: will it rain? Is it sunny? (Bad example, the value "maybe" is not a Boolean value in meteorology !).

No arithmetic operations can be performed on boolean values (as for nil values). You can modify our example and see what happens when the print(true) or print(false) function is called. This will display the word "true" or "false" on the screen for the lazier of you.

The number type

It's easier this time. The number type represents numeric data, whether real, integer, negative, positive, ... Let's try something that itches your brain right away and see what happens when we add number values in our example:

print(0.8 + 5) sys.beep()

Run the program and see the message printed to the screen, followed by the bip :


Great! You have done your first artihmetic operation in Lua ! As you can see, the result of a mathematical expression in Lua is a value (here the number 5.8) that can serve as argument of a function. As an example, let's do more arithmetic:

print(0.8 + 5) -- addition print(2 - 4) -- soustraction print(10 / 5) -- division

Run the program and see the messages printed to the screen (I must admit, I hate bips now !):

5.8 -2 2.0

I would like to make a few remarks at this point. First of all, Lua knows how to calculate with numbers, and there are still many other operations it can perform.

You will notice that I added some text next to each call to the print() function. These are comments that are not interpreted because they begin with the two characters "--", so they are useless except to describe / comment the program.

Finally, you will notice that dividing integers produces a real number. But 2.0 and 2 are the same value, so it doesn't matter. It must, however, be taken into account when displaying it, depending on what you want to display.

Lua standard Library contains mathematic functions that operate on numbers. They are in the "math" aisle of the runtime library. See the Lua 5.4 manual for a complete listing.

The string type

You already have used character strings in Lua, with your first program. The first line called the function print() with a double quoted sentence "Listen!". In fact this sentence is a string.

Strings consist of a series of characters. Indeed strings are only containers for binary data. Lua doesn't even know what a character is, but it is way beyond the scope of this tutorial. Currently, consider a string to be a sequence of characters, with a limitation when using accented characters (Luart makes it easy to use strings with accented characters, see How to use UTF8 strings for more details).

Lua allows string-to-number operations if the string contains only numeric characters. Lua then automatically converts strings into numbers, and vice versa. Try by rewriting myprogram.lua with :

print("0.8" + 5) -- addition print( 2 - "4") -- soustraction print("Hello ".."world !") -- concatenation print('Hello' / 5) -- division

Run the program and see the results :

5.8 -2 Hello world ! myprogram.lua:4: attempt to div a 'string' with a 'number'

The error message is clear: be careful with that feature of Lua : when doing arithmetic between numbers and strings, be sure that the strings contains only numeric characters.

There is a way to chain strings together to make a new one. You have probably noticed that. Thanks to the two dot ".." operator, this operation, called concatenation, is possible. A string can be concatenated with a number (but not the reverse), always with the same vigilance.

Lua standard Library contains functions that operate on strings. They are in the "string" aisle. See the Lua 5.4 manual for a complete listing. Luart implements more strings capabilities, see the Luart string module documentation.

For now, that's all. Complex types will be covered in the next tutorial! Thanks for reading !