User Tools

Site Tools


lgscript:lua_types

Types and Values

Basic types are number, string, boolean, and nil. The number type is any real number Lua does not distinguish between integers and numbers with fractional parts. A string may be used where a number is expected. If the string can be interpreted as a number, the interpreted value will be used, otherwise an error will occur. A number may be used where a string is expected and it will be converted to a decimal representation of the value. There is no automatic conversion to or from boolean or nil values.

Numbers are written using decimal digits with an optional fractional part and optional exponent. You can also write an integer in base-16 by using the prefix 0x followed by hexadecimal digits. A number may be prefixed with - to make it negative. There is no corresponding positive operator.

  3 3.0 3.1416 314.16e-2 -0.31416E1 0x123456789abcdef -0X56

Strings are arrays of characters, including special non-printing characters such as new-line (‘\n’), and even null characters (‘\0’). A string is written by enclosing the array of characters with a pair of matching single or double quotes. Non-printing characters in a quoted string can be written as an escape sequence.

Escape Character
\a Bell (ASCII 7)
\b Backspace (ASCII 8)
\f Form feed (ASCII 12)
\n New line (ASCII 10)
\r Carriage return (ASCII 13)
\t Tab (ASCII 9)
\v Vertical tab (ASCII 11)

The escape sequence \x begins a hexadecimal character code. Follow the escape with one or two hexadecimal digits. An escape sequence of a backslash followed by one, two, or three decimal digits is a decimal character code. The valid range for decimal character codes is 0-255. Be careful that if you want to follow a decimal character code with a decimal digit character, then you must use three digits in the escape sequence.

A backslash followed by any character that doesn’t form an escape sequence represents just the second character. This is how you include single or double quotes in a quoted string. If the backslash is the last character in a line, then a new-line character is put in the string.

A long bracket that isn’t part of a comment is a long string. The characters in the bracket are the contents of the string as-is. Escape sequences are not translated in long strings.

-- All of these strings are the same.
a = 'alo\n123"'
a = "alo\n123\""
a = '\x61lo\10\04923"'
a = [[alo
123"]]
a = [==[
alo
123"]==]

A boolean has one of two values: false and true. There is only one nil value: nil. Where a boolean is expected, a nil value is treated as false. Any value that is not false or nil is treated as true. This means that the number 0 and the empty string are both considered true.

Basic types are passed by value. When you assign the value of a variable to another variable, the two variables are distinct with values that compare as equal. Object types are passed by reference. When an object is assigned to two variables, both of the variables refer to the same value. Changes to the value of one of the variables will be reflected in the other variable. Object types are table and userdata.

Other than the object types, functions and threads are values that are passed by reference. These types are specialized objects and do not have metatables.

The type of a value is associated with its data, not the variable it is assigned to. If you change the type of a variable, you are assigning a new value to it.

lgscript/lua_types.txt · Last modified: 2011/05/15 21:30 by telliamed