-
Notifications
You must be signed in to change notification settings - Fork 1
OLoad.lua
Pixel edited this page Jul 5, 2018
·
1 revision
OLoad.lua is a super simple function for creating function overloads. It's not the best in any regards, but it's there. Usage is as follows:
local overLoad = require("OLoad")
-- create an overloadable function:
local overLoadedFunction = overLoad(
-- a function overload
function(a,b,c)
return "number","string","table"
end,
-- the arguments for the previous overload
{"number","string","table"},
-- repeat for every initial overload
)
-- add a version to an overloaded function:
overLoadedFunction:add(
function(a,b,c)
return "string", "table", "number"
end,
{"string","table","number"}
-- 'add' can handle multiple functions, simply repeat this pattern for each.
)
-- add a default version to the overloaded function:
overLoadedFunction:add(
function(...)
local out = {}
for i,v in ipairs({...}) do
out[i] = type(v)
end
return unpack(out)
end
)
-- execute an overloaded function:
print(
overLoadedFunction(1,"str",{}) -- calls the correct version of the function for the given arg types
) -- prints "number string table"