Skip to content

Instantly share code, notes, and snippets.

@Fingercomp
Last active April 19, 2016 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fingercomp/3975221042e4e16c83d7a5f6fceed0d1 to your computer and use it in GitHub Desktop.
Save Fingercomp/3975221042e4e16c83d7a5f6fceed0d1 to your computer and use it in GitHub Desktop.
local checkArg = checkArg or function(pos, value, ...)
local exp = {...}
for _, tp in pairs(exp) do
if type(value) == tp then
return
end
end
if #exp == 1 then
exp = exp[1]
elseif #exp == 2 then
exp = table.concat(exp, " or ")
elseif #exp > 2 then
exp = table.concat(exp, ", ", 1, #exp - 1) .. ", or " .. exp[#exp]
end
error("bad argument #" .. pos .. ": " .. exp .. " expected, got " .. type(value))
end
return function(class, name, func, doc)
checkArg(1, class, "table")
checkArg(2, name, "string")
checkArg(3, func, "function")
checkArg(4, doc, "string", "nil")
local method = setmetatable({},{
__call = function(self, ...)
return func(...)
end,
__tostring = function(self)
return doc
end,
__index = method
})
class[name] = method
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment