【发布时间】:2020-07-28 04:18:39
【问题描述】:
如何在可选的lua函数中传递表。
例如
function test(options)
local a = options.a
end
这个功能应该同时工作
test(options)
和
test()
【问题讨论】:
如何在可选的lua函数中传递表。
例如
function test(options)
local a = options.a
end
这个功能应该同时工作
test(options)
和
test()
【问题讨论】:
function test(options)
options = options or {}
local a = options.a or 0 -- or whatever it defaults to
end
您只需or 可选值及其默认值。
如果尚未提供该值并因此为nil,它将解析为ored 值。
这是一个较短的版本
function test(options)
if not options then
options = {}
end
local a = 0
if options.a then
a = options.a
end
end
【讨论】:
options.a 是实际的false 值之外,在这种情况下or 技巧不会产生正确的结果。最好明确检查nil。
or false 作为默认值