public
Last active

Lua script for fancy named parameters with default values

  • Download Gist
example.lua
Lua
1 2 3 4 5 6 7 8
local fp = require "fancyparams"
 
myfunction = fp(
{{"a"},{"b",7},{"c",5}},
function(a, b, c)
print(a, b, c)
end
)
fancyparams.lua
Lua
1 2 3 4 5 6 7 8 9 10 11 12 13
local function fancyparams(arg_def,f)
return function(args)
local params = {}
for i=1, #arg_def do
local paramname = arg_def[i][1] --the name of the first parameter to the function
local default_value = arg_def[i][2]
params[i] = args[i] or args[paramname] or default_value
end
return f(unpack(params,1,#arg_def))
end
end
 
return fancyparams

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.