Values & Types
In Lua, variables do not have types, values do! This is what is called a dynamically typed language. So, there are no type definitions used when declaring variables, the values are used to identify the types. All of these values can be stored in variable. Any variable can be passed into a function as parameters, and any values can be returned as results to the function!
So, what about types? In Lua there are 8 basic types of values: nil, number, string, boolean, function, table, user data, and thread. Lets take a look at each of the Lua data types in a little more detail.
In Lua an uninitialized value is nil. nil is essentially used to represent nothing or an invalid state of the variable; no useful data. nil has a single value nil, which mainly separates the value from any other values.
Lua uses the type number for both integers and floating-point values. Integers are whole numbers that do not posses a decimal point. Integers can be expressed as either decimal (base 10) values as in: x = 10. They can also be expressed in hexadecimal using "0x" syntax, as in w = 0xFF.
Floating point values are values that contain decimal points. One thing to remember in Lua is that no matter the contents of the number value, Lua automatically converts between integers and floats. This can be a really powerful feature allowing the programmer to ignore the differences between floats and integers, however, be careful because performing operations on a mix of integers and floats may produce some unexpected results.
In the microMighty embedded framework, both integers and floats are represented as 32-bit values.
Lua strings are immutable sequences of 8-bit data (bytes). Strings can represent a sequence of any 8-bit data, even including embedded zero values. Lua also doesn’t impose encoding on your strings, so any encoding scheme can be used. For example, “This is a string” is a pretty obvious string, but “\x48\x45\x4c\x4c\x4f\x20\x58\x4f\x52\4c\x44” is also a string.
Boolean values are values that can be either true or false. Nil values are treated as false and all other values are treated as true. Boolean values are most often associated with comparisons, however they can be used for things such as conditional assignments and option flags.
Values with the function type are associated with Lua functions, and functions from the internal “C” language functions that can be called from Lua. A great function to experiment with is the print() function which allows quick and simple printing of values. Try Entering the code below:
What happens? Lua calls the built in print function and passes the string “Hello World”, and the function sends the string to the USB serial interface. If this is all confusing, don’t worry because functions are going to be covered with more detail later on in the manual.
Tables are lists of data (arrays). They are associative so each element in a table has an associated key that can be used to reference the stored value. Tables can be simple or can get very complex since they are the only structured data mechanism in Lua and they facilitate creation of records, structures, and objects.
Fun fact: Tables can contain 7 of the 8 Lua types including other tables! Which is the only type that can’t appear in a table? Only nil values can’t be stored in tables.
The type user data is associated with “C” language data that is passed back and forth between the “C” language core functions of Callisto and the Lua code running in the microMighty environment. You won’t be creating any user data values unless you're editing the core code of microMighty. You may find this type used in your extensions.
Two common user data items with which you will interface are the pin objects and serial port (com0 - com6) objects.
Lua supports basic multi-threading through coroutines. Coroutines are an advanced topic covered in later sections so this type will be covered in much more detail in these sections.