简体   繁体   English

检测lua中的单个nil参数

[英]detecting single nil argument in lua

Is it possible to distinguish the call 是否可以区分呼叫

myFunc()

from

myFunc(nil)

inside the function myFunc? 在函数myFunc里面?

It is actually possible to distinguish a nil value from no value in Lua functions, providing they are expecting a variable number of arguments with the ... operator. 实际上可以区分Lua函数中的nil值和no值,只要它们期望使用...运算符的可变数量的参数。 It is not necessary very easy nor reasonable to exploit this, though. 但是,利用它并​​不是非常容易也不合理。 Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM