gpt4 book ai didi

lua - 为什么 "not nil"在 Lua 中返回 true?

转载 作者:行者123 更新时间:2023-12-04 19:05:39 25 4
gpt4 key购买 nike

我使用 repl.it 来查看它返回的内容,只是出于好奇,结果发现不是零返回 true

为什么会这样?是不是因为在 Lua 中一切最终都应该是对的还是错的?

Repl.it 链接:https://repl.it/repls/SanePastelHarrier

最佳答案

因为 nil 在转换为 bool 值时是 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 将其参数视为 bool 值:

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.

关于lua - 为什么 "not nil"在 Lua 中返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47963048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com