Skip to content

Instantly share code, notes, and snippets.

@Fingercomp Fingercomp/addmethod.lua Secret

Last active Apr 19, 2016
Embed
What would you like to do?
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
You can’t perform that action at this time.