Types

In LUA una variabile può contenere uno dei diversi tipi di dati: nil, numeri, stringhe, tabelle, funzioni e oggetti. Vale nil (valore nullo) una variabile, per esempio, alla quale non è stato ancora assegnato un valore. Nelle espressioni booleane nil è l´unico valore considerato falso, ogni altro valore, compreso lo zero, è vero. I numeri sono considerati sempre reali, non esiste distinzione tra interi e numeri con la virgola.

  • nil
  • Type of nil , which is different from any other value.
  • number
  • Double-precision (64 bit) floating-point numbers
  • string
  • Character strings. Strings may contain any 8-bit character, including embedded nulls.When used for text, strings are platform- and locale-dependent.
  • function
  • Functions are first-class values in Lua. They can be stored in variables, passed as arguments, and returned as results. Functions have two different tags.All Lua functions have the same tag, and all C functions have the same tag.
  • userdata
  • This type is provided to allow arbitrary C pointers to be stored in Lua variables; corresponds to a void* and has no pre-defined operations in Lua, except assignment and equality.
  • table
  • This type implements associative arrays.Arrays can be indexed with any value (except nil).Use table to represent ordinary arrays, symbol tables, sets, records, graphs, trees, etc. This is the main data structuring mechanism in Lua.

Each of the types nil, number, and string has a different tag.All values of each of these types have the same pre-defined tag.Values of type function can have two different tags, depending on whether they are Lua functions or C functions.Values of type userdata and table can have variable tags, assigned by the programmer.

The tag function returns the tag of a given value.User tags are created with the function newtag. The settag function is used to change the tag of a table. The tag of userdata values can only be set from C. Tags are mainly used to select tag methods when some events occur. Tag methods are the main mechanism for extending the semantics of Lua.