Lua 类型教程 |
|
请参考 TutorialExamples 来了解如何运行这里提供的示例。我们将使用 print() 函数来输出值或对这些值进行的计算。参数周围的括号很重要,省略它们会导致错误。
> print("hello") -- print the string hello. hello
数字类型表示浮点数(带小数的数)。自 5.3 版本起,整数(无小数的数)有了单独的内部表示。但是,它们也被认为是“数字”类型。
Lua 允许使用常规的运算符对数字进行简单的算术运算,例如加、减、乘和除。
> print(2+2) 4 > print(2-7) -5 > print(7*8) 56 > print(7/8) 0.875
请注意,数字没有被四舍五入成整数。它们是浮点数,也就是实数。我们可以使用 = 运算符将值赋给变量。
> x = 7
> print(x)
7
= 运算符将数字 7 赋给变量 x。我们再次使用 print() 函数来输出 x 的值。现在我们可以使用 x 中的值进行其他计算。> x = x * 9 > print(x) 63 > print(x*2) -- will not change the value of x 126 > print(x) 63
关于 Lua 数字类型的更多信息,您可以查阅 NumbersTutorial。
Lua 也使用字符串(即文本)类型。要创建字符串,请将文本括在 "双引号" 或 '单引号' 中。
> print("hello") hello
> who = "Lua user" > print(who) Lua user
.. 运算符将字符串连接(合并)在一起。> print("hello ") hello > print("hello " .. who) -- the variable "who" was assigned above hello Lua user > print(who) Lua user
与其他一些语言不同,您不能使用 + 运算符来连接字符串。例如:
> message = "hello " + who stdin:1: attempt to perform arithmetic on a string value stack traceback: stdin:1: in main chunk [C]: ?
关于 Lua 字符串类型的更多信息,您可以查阅 StringsTutorial。
布尔值要么是 true,要么是 false。如果一个值不是 true,那么它一定是 false,反之亦然。not 运算符可以放在布尔值前面来反转它。例如,not true 等于 false。
> x = true > print(x) true > print(not x) false > print(not false) true
== 和不等于 ~= 运算符将根据提供给它们的值返回布尔值。> print(1 == 0) -- test whether two numbers are equal false > print(1 == 1) true > print(1 ~= 0) -- test whether two numbers are not equal true > print(true ~= false) -- is true not equal to false? true
请注意,赋值使用单个等号 (=),而比较使用双等号 (==)。这两个运算符有不同的含义但看起来相似,写错其中一个是很常见的错误。
关于 Lua 布尔类型的更多信息,您可以查阅 ExpressionsTutorial。
Lua 有一种通用的“聚合”数据类型,称为表。聚合数据类型用于存储包含其他对象的集合(如列表、集合、数组和关联数组),这些对象本身也可以是数字、字符串,甚至是其他聚合类型。Lua 的独特之处在于,它使用表(它们是关联数组)来表示所有其他聚合类型。
表使用一对花括号 {} 创建。让我们创建一个空表。
> x = {}
> print(x)
table: 0035C910
TablesTutorial 将在稍后解释如何使用表。
在 Lua 中,函数就像数字和字符串一样被赋给变量。函数使用 function 关键字创建。这里我们创建一个简单的函数,它会打印一条友好的消息。
> foo = function () print("hello") end -- declare the function > foo() -- call the function hello > print(foo) -- get the value of the variable "foo" function: 0035D6E8
foo 的值,它会显示(与表类似)该值是一个函数,并且有一个表示该特定函数的唯一标识符。因此,作为与其他值相同的普通值,我们应该能够像其他值一样将函数赋给变量,我们也可以做到。> x = function() print("hello") end > x() hello > print(x) function: 0035EA20
函数可以成为表的一部分。
> a = "aeiou" -- a string > b = 13 -- a number > c = function() -- a function > print ("\n\n\tAin't it grand") > end > d = { a, b ,c} -- put them in a table > function printit(tata) -- print their types. >> for key, value in ipairs(tata) do print(key, type(value)) end >> end > printit(d) 1 string 2 number 3 function
FunctionsTutorial 将在稍后解释如何使用函数。
nil 是一个特殊值,表示没有有用的值。如果您尝试获取一个不存在的变量,您将得到 nil。
> print(x) nil > x = 2.5 > print(x) 2.5
nil 在其他地方的使用将在其他教程中展示。
用户数据(Userdata)值是 Lua 之外的对象,例如用 C 实现的对象。当 C 库中的一个对象暴露给 Lua 时,通常会产生这些。您无法在 Lua 中对用户数据值做任何事情,除了传递它,它只用于传递给同一个 C 库暴露给的用户数据函数。但是,通过使用元方法(将在后续教程中解释),可以使用户数据与运算符一起工作,并表现得类似表。用户数据是一个更高级的主题,在 [Lua 参考手册] 中有更详细的讨论。
线程(Thread)值代表一个独立的(协作式)执行线程。这些将在 CoroutinesTutorial 中进一步讨论。
您可能已经注意到,在我们创建上述变量时,我们没有指定要创建哪种类型的变量。例如:
a = 1
b = "hello"
c = {}
a = 1
a = "hello"
a = {}
由于 Lua 是一种“*反射性*”语言,我们可以使用 Lua 函数 type() 来获取特定对象类型的描述。
> x = "123" -- a string > print(x, type(x)) -- show the value of x and its type 123 string > x = x + 7 -- add a number to the string which forces coercion > print(x, type(x)) -- again show the value and type 130 number