# basic type
- Including nil, string, boolean, number, integer, function, table, Map, Array, Stream several basic types
- nil: indicates empty data
- string: String, the literal in the code is surrounded by double quotes or single quotes or [[ and ]], such as "hello world",'hello', [[hello world]]. [[And]] includes cross-line strings
- boolean: Boolean type, indicating true or false, true or false
- integer: integer types, such as 123,45 this support 64bit integers, integers ranging -9223372036854775808to9223372036854775807
- number: floating point numbers, the fractional, is implemented in 64bit double type, range of values is -2^1024to 2^1024, accuracy 15 decimal places.
- At the same time, the integer type at compile time can be implicitly converted to the number type, and the number type cannot be implicitly converted to the integer type
- function: Function type, the function can be used as the value of the variable, can be used as the parameter and the return value, support closure, use similar to other types of values, but can be called
- Map
: Hash table type. T represents the value type of the hash table. It is a key-value pair. The key type is a string type. The value type of each key-value pair is the type represented by T. The type is a Map subtype of table type. - Map
Types can be operated by functions in the table module, and can be accessed by reading and writing through brackets or subscripts or dots plus attribute names, such as a['name'] , a.name, etc. *Array : List type, T represents the value type of the list, each item in the list is the type represented by T, and the Array type is a subtype of the table type - Array
The type can be operated by the function in the table module, and can be read and written by subscript in brackets, such as a[1], etc. - Stream Binary byte stream type, representing a stream of binary bits
such as
let a = {}
a['name'] = 'glua'
let b = { age: 2 }
let c={ ['name']='China', age=5000, address: 'China' }
let d = [ 1, 2, 3 ]
- record: The custom data structure at compile time, similar to the C language struct structure, which can be customized attributes, the program is compiled, and the variable of record type at run time is equivalent to the table type
such as
type Person = {
name: string,
age: int default 24,
age2: int = 24
}
table: Map
and Array the parent type of the record type, you can implicitly convert the other three types or explicitly to the table type through the totable function, you can also implicitly convert the table type to a specific subtype, the Map type can be converted to the record type , Other different subtypes cannot be directly converted to each other union: indicates that it may be one of several different types at compile time, such as let a: int | string | number indicates that it is of type int or string or number
Literal type: Literal Type is an enumerated type. You can use multiple strings, numbers, Boolean values, or nil literals as each item of the enumerated type. When assigning values to the enumerated type, you must use a compatible enumerated type. Or the literal value in the enumeration type, otherwise an error is reported during compilation
such as
type Gender = "male" | "female"
var gender: Gender
gender = "male" -- Correct
gender = "Chinese" -- Error:
- object: Compile-time type, is the parent type of all types, variables are declared as object can be assigned to any type of value, and access to the attributes of object type variables is allowed at compile-time.
such as
var a: object = 1
a = "hello"
pprint(a.name)