Lua nil 类型的条件

Lua conditionals on nil types

问题

Lua中:

local a = b or 0
local a = b and 1 or 0

其中 b 可以是任何类型。

代码行之间有什么区别? 在什么情况下我会使用其中一种?


上下文

我必须将现有的 Lua 代码移植到另一个服务,我 运行 遇到了一个问题,理解为什么在代码的某些部分(我不是 Lua 开发人员) ,一个变量被分配给一个,而在代码的其他部分,一个变量被分配给另一个。右边的变量是一个输入参数,我无法知道预期的类型。


我试过的

我在网上查找了有关此问题的 Lua 文档,但找不到任何明确的答案。我运行自己测试:

local a1;
print(type(a1))               -- nil
local b1 = a1 or 0            
print(b1 .. " " .. type(b1))  -- 0 number
local c1 = a1 and 1 or 0
print(c1 .. " " .. type(c1))  -- 0 number

local a2 = 5
print(type(a2))               -- number
local b2 = a2 or 0          
print(b2 .. " " .. type(b2))  -- 5 number
local c2 = a2 and 1 or 0
print(c2 .. " " .. type(c2))  -- 1 number

local a3 = 0
print(type(a3))               -- number
local b3 = a3 or 0          
print(b3 .. " " .. type(b3))  -- 0 number
local c3 = a3 and 1 or 0
print(c3 .. " " .. type(c3))  -- 1 number

local a4 = false
print(type(a4))               -- boolean
local b4 = a4 or 0
print(b4 .. " " .. type(b4))  -- 0 number
local c4 = a4 and 1 or 0
print(c4 .. " " .. type(c4))  -- 0 number

local a5 = true
print(type(a5))               -- boolean
local b5 = a5 or 0
print(b5 .. " " .. type(b5))  -- error, concatenating boolean to string
local c5 = a5 and 1 or 0
print(c5 .. " " .. type(c5))  -- 1 number

local a6 = "str"
print(type(a6))               -- string
local b6 = a6 or 0
print(b6 .. " " .. type(b6))  -- str string
local c6 = a6 and 1 or 0
print(c6 .. " " .. type(c6))  -- 1 number

local a7 = ""
print(type(a7))               -- string
local b7 = a7 or 0
print(b7 .. " " .. type(b7))  --  string
local c7 = a7 and 1 or 0
print(c7 .. " " .. type(c7))  -- 1 number

在我看来,带有 and 条件的代码行的唯一用例是当 b 是 boolean 或 nil 类型并且 a 应该导致 0当 bnilfalse 和 1 当 b 是 true.

在 Lua 中,这些是选择运算符,具有短路评估。

falsenil是"falsey";任何其他值都是 "truthy." 除了 "falsey",操作数类型无关紧要,结果类型不一定是 "boolean".

  • or 选择 (returns) 第一个真值操作数。

  • and 如果第一个操作数为假,则选择第一个操作数,否则选择第二个操作数。它的优先级高于 or.

这导致了几个成语:

b or 0 -- default to 0
t = t or {} -- existing or new, empty table
b and 1 or 0 -- coerce to 1, defaulting to 0

你的两个例子之间的区别是,第二个强制为 1,而第一个让 a "truthy" b 成为。