2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Preface
This article is referenced from:Lua data types | Novice tutorial (runoob.com)
If you want to know more details, please check the link above:
- print(type("Hello world")) --> string
- print(type(10.4*3)) --> number
- print(type(print)) --> function
- print(type(type)) --> function
- print(type(true)) --> boolean
- print(type(nil)) --> nil
- print(type(type(X))) --> string
- print(type(Y)) -->nil
- print(type('Y')) -->string
You can see that the type(Y) we output here is a nil type. Why? The following nil (empty) will demonstrate it to you.
The nil type represents a type without any valid value. It has only one value - nil. For example, printing a variable without a value will output a nil value:
As follows: The output types of the three variables that have not been assigned initial values are all nil
- tab1 = { key1 = "val1", key2 = "val2",
- "val3","val4",key3 = "val5" }
- for k, v in pairs(tab1) do
- print(k .. " - " .. v)
- end
- print("=========直接输出=============")
-
-
- tab1.key1 = nil
- for k, v in pairs(tab1) do
- print(k .. " - " .. v)
- end
- print("============删除key1后==========")
-
-
-
- tab1.key3 = nil
- for k,v in pairs(tab1) do
- print(k.. " - "..v)
- end
- print("=========删除key3后============")
-
For global variables and tables, nil also has a "deletion" effect. Assigning a nil value to a global variable or a variable in a table is equivalent to deleting it.
type(X)==nil results infalseThe reason is that type(X) actually returns the string "nil", which is a string type:
The boolean type has only two optional values: true and false. Lua considers false and nil to be false, and all others to be true. The number 0 is also true:
- print(type(true))
- print(type(false))
- print(type(nil))
-
- if false or nil then
- print("至少有一个是 true")
- else
- print("false 和 nil 都为 false")
- end
-
- if 0 then
- print("数字 0 是 true")
- else
- print("数字 0 为 false")
- end
By default, Lua has only one number type - double (double precision) type (the default type can be modified in luaconf.h). The following writing methods are all considered as numbers.
When performing arithmetic operations on a numeric string, Lua will try to convert the numeric string into a number:
In Lua, table creation is done through "construction expression". The simplest construction expression is {}, which is used to create an empty table. You can also add some data to the table to initialize the table directly:
A table in Lua is actually a"Associative Arrays"(associative arrays), the index of the array can be a number or a string.
The length of a table is not fixed. When new data is added, the length of the table will automatically increase. The value of an uninitialized table is nil.
In Lua, functions are considered "first-class values".
In Lua, the main thread is the coroutine. It is similar to a thread and has its own independent stack, local variables and instruction pointer.
Can share global variables and most other things with other coroutines.The difference between threads and coroutines: threads can run multiple times at the same time, but only one coroutine can run at any time, and a running coroutine will only be suspended when it is suspended.
Userdata is a user-defined data type used to represent a type created by an application or C/C++ language library.
You can store any C/C++ data of any data type (usually struct and pointer) into a Lua variable.