是否可以区分呼叫
myFunc()
Run Code Online (Sandbox Code Playgroud)
从
myFunc(nil)
Run Code Online (Sandbox Code Playgroud)
在函数myFunc里面?
实际上,可以nil在Lua函数中区分值和无值,前提是它们期望与...运算符有可变数量的参数.但是,利用它并不是非常容易也不合理.例:
function myFunc(...)
if select('#', ...) == 0 then
print "Called without argument"
elseif select('#', ...) == 1 and select(1, ...) == nil then
print "Called with a nil argument"
end
end
Run Code Online (Sandbox Code Playgroud)