Lua 检查参数类型
示例
某些函数仅适用于某种类型的参数:
function foo(tab) return tab.bar end --> returns nil if tab has no field bar, which is acceptable --> returns 'attempt to index a number value' if tab is, for example, 3 --> which is unacceptable function kungfoo(tab) if type(tab) ~= "table" then return nil, "t使你没用 " .. type(tab) .." somewhere else!" end return tab.bar end
这有几个含义:
print(kungfoo(20)) --> prints 'nil, t使你没用 number somewhere else!' if kungfoo(20) then print "good" 其他打印 "bad" end --> prints bad foo = kungfoo(20) or "bar" --> sets foo to "bar"
现在,我们可以使用所需的任何参数来调用函数,并且不会使程序崩溃。
-- if we actually WANT to abort execution on error, we can still do result = assert(kungfoo({bar=20})) --> this will return 20 result = assert(kungfoo(20)) --> this will throw an error
那么,如果我们有一个函数可以对特定类的实例做些什么呢?这很困难,因为类和对象通常是表,所以type函数将返回'table'。
local Class = {data="important"} local meta = {__index=Class} function Class.new() return setmetatable({}, meta) end -- this is just a very basic implementation of an object class in lua object = Class.new() fake = {} print(type(object)), print(type(fake)) --> prints 'table' twice
解决方案:比较元表
-- continuation of previous code snippet Class.is_instance(tab) return getmetatable(tab) == meta end Class.is_instance(object) --> returns true Class.is_instance(fake) --> returns false Class.is_instance(Class) --> returns false Class.is_instance("a string") --> returns false, doesn't crash the program Class.is_instance(nil) --> also returns false, doesn't crash either