示例工程 06-control-flow-and-truthiness

06-control-flow-and-truthiness/04-and-or-false-trap/main.lua

lua · 0.5 KB

-- @runtimes lua54 luajit
--- 演示:a and b or c 在 b 为 false 时返回 c。
 
local M = {}
 
--- @return table { trap = any, fixed = any }
function M.run(log)
	log = log or print
	local enabled = true
	local flag = false
	local trap = enabled and flag or "fallback"
	local fixed = enabled and { value = flag } or { value = "fallback" }
	log(("trap=%s"):format(tostring(trap)))
	log(("fixed_is_false=%s"):format(tostring(fixed.value == false)))
	return { trap = trap, fixed = fixed.value }
end
 
if not ... then
	M.run()
end
 
return M