为什么 "not nil" return 在 Lua 中为真?
Why does "not nil" return true in Lua?
出于好奇,我已经使用 repl.it 来查看它返回的内容,结果是
not nil
返回 true
这是为什么?是不是因为 Lua 一切最终都应该是真或假?
Repl.it link: https://repl.it/repls/SanePastelHarrier
因为nil
转换为布尔值时是false
:
2.2 Booleans
The boolean type has two values, false
and true
, which represent the traditional boolean values. However, booleans do not
hold a monopoly of condition values: in Lua, any value may represent a
condition. Conditionals (such as the ones in control structures)
consider both false
and nil
as false
and anything else as true
.
Beware that, unlike some other scripting languages, Lua considers both zero
and the empty string as true
in conditional tests.
并且 not
将其参数视为布尔值:
3.3 Logical Operators
The logical operators areand
, or
, and not
. Like control structures, all logical operators consider both false
and nil
as false
, and anything else as true
.
出于好奇,我已经使用 repl.it 来查看它返回的内容,结果是
not nil
返回 true
这是为什么?是不是因为 Lua 一切最终都应该是真或假?
Repl.it link: https://repl.it/repls/SanePastelHarrier
因为nil
转换为布尔值时是false
:
2.2 Booleans
The boolean type has two values,
false
andtrue
, which represent the traditional boolean values. However, booleans do not hold a monopoly of condition values: in Lua, any value may represent a condition. Conditionals (such as the ones in control structures) consider bothfalse
andnil
asfalse
and anything else astrue
. Beware that, unlike some other scripting languages, Lua considers both zero and the empty string astrue
in conditional tests.
并且 not
将其参数视为布尔值:
3.3 Logical Operators
The logical operators are
and
,or
, andnot
. Like control structures, all logical operators consider bothfalse
andnil
asfalse
, and anything else astrue
.