在lua中检测单个空参数

在函数myFunc内是否可能区分调用

myFunc()

myFunc(nil)

原文链接 https://stackoverflow.com/questions/12622912

点赞
stackoverflow用户1008957
stackoverflow用户1008957

在 Lua 函数中,如果期望通过 ... 运算符接收可变数量的参数,则实际上可以区分 nil 值和没有值。然而,利用这一点并不是非常容易或合理的。例如:

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
2012-09-27 15:59:01