Shared: option to pass parameters as a simple list or as named parameters

Some of my functions have a long list of parameters, and it is easy to make mistakes when setting them. It would be nice to have the option to use named parameters, especially when I only want to set a couple of them.

The code below allows you to provide either a simple list of (unnamed) parameters, or a named list, in a table. Your function then only needs a single extra line of code to call the Params function to unpack the parameters correctly, by providing a list of the parameter names in the correct order.

Suggestions and improvements welcomed.

function setup()
    --this is how you'd normally do it
    Test1(11,22,nil,44,55)

    --this is my proposed approach, allowing named parameters as well
    --both alternatives below give the same result
    --(a) pass parameters as simple list, in the correct order
    Test2(11,22,nil,44,55)
    --(b) pass only the named parameters we want, in any order
    Test2({stealth=55,health=11,dexterity=44,strength=22}) --strength omitted
end

--usual function requires a list in the right order
function Test1(h,s,d,m,t)
    print(h,s,m,d,t)  --test result
end

--this function allows named parameters as well
function Test2(...)
    --unpack the parameters, using the names to sort them correctly
    --this is the only extra line you need in your function
    local h,s,m,d,t=Params({"health","strength","magic","dexterity","stealth"},arg) 
    print(h,s,m,d,t)  --test result
end

function Params(list,tbl) --tbl is params
    if type(tbl[1])=="table" then --was named table
        local t={} --match the named params with the list provided
        for n,v in pairs(tbl[1]) do
            for i=1,#list do
               if n==list[i] then t[i]=v break end 
            end     
        end  
        return unpack(t)
    else --was an unnamed list, return it as is
       return unpack(tbl) 
    end
end