nil 类型的 Lua 条件

Pho*_*reo 4 null lua conditional-statements

问题)

Lua

local a = b or 0
local a = b and 1 or 0
Run Code Online (Sandbox Code Playgroud)

其中 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
Run Code Online (Sandbox Code Playgroud)

在我看来,带有and条件的代码行的唯一用例是当bis 为 boolean 或 nil 类型时,当is ora时结果应为 0 ,当 b is 时应结果为 1 。bnilfalsetrue

Tom*_*get 6

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

false并且nil是“虚假的”;任何其他值都是“真实的”。除了“falsey”之外,操作数类型并不重要,并且结果类型也不一定是"boolean"

  • or选择(返回)第一个真实操作数。

  • and如果第一个操作数为 false,则选择第一个操作数,否则选择第二个操作数。它的优先级高于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
Run Code Online (Sandbox Code Playgroud)

你的两个例子之间的区别是第二个强制为 1,而第一个让“真实”b 成为。