我使用 repl.it 来查看它返回的内容,只是出于好奇,结果发现
不是零
返回 true
为什么会这样?是不是因为在 Lua 中一切最终都应该是对的还是错的?
Repl.it 链接:https://repl.it/repls/SanePastelHarrier
最佳答案
因为 nil
在转换为 bool 值时是 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
将其参数视为 bool 值:
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
.
关于lua - 为什么 "not nil"在 Lua 中返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47963048/