我想在 Lua 4 中使用这个函数:
function ternary(cond, T, F)
if cond then return T else return F end
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下:
loadHW1 = false
print(ternary(loadHW1 == true, "this should not appear", nil))
Run Code Online (Sandbox Code Playgroud)
但是,文本总是在我期望结果为nil. 我究竟做错了什么?谢谢。
[编辑]
我切换到这个,但仍然得到“这是真的”结果:
loadHW1 = 0
print(ternary(loadHW1, "this is true", "this is false"))
Run Code Online (Sandbox Code Playgroud)
Lua 4 没有布尔值:它们是在 Lua 5 中引入的。
在 Lua 4 中,只有 nil 是假的;其他任何东西,包括 0,都是真的。
在您的代码中,
false和true被解释为未定义的全局变量,因此都评估为 nil。因此,loadHW1 == true变成nil == nil,这是真的,因此ternary接收到 1 cond。
如果要在 Lua 4 中使用false和true,请按如下方式定义它们:
false = nil
true = 1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |