获取值

基本类型 / 布尔类型

local name = "Alex"
-- 比较得到布尔值
local name_exists = name ~= ""
-- name_exists = true

local num = 7
local is_ten = num == 10
-- is_ten = false

-- not not 把任何值转成布尔值
local from_number = not not 42
-- from_number = true

-- 陷阱:空表也是 true
local from_table = not not {}
-- from_table = true

-- 只有 nil 和 false 得到 false
local from_nil = not not nil
-- from_nil = false

print("name_exists = " .. tostring(name_exists))
print("is_ten = " .. tostring(is_ten))
print("from_number = " .. tostring(from_number))
print("from_table = " .. tostring(from_table))
print("from_nil = " .. tostring(from_nil))