lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Dear Lua list,

Suppose I have a function from a third party that I cannot alter.  It
takes a number (>1) of arguments, something like:

function foo (a, b)
  print ("You supplied " .. a .. " and " .. b)

I'd like to be able to store the function as one value, and all the
parameters for a future call in another single value:

-- store parameters for a future call to "foo(23, 42)"
f = {func=foo, params={23, 42}}

If I did this, how would I go about calling the function with my
stored parameters?  If I wrote "f.func(f.params)" it would be wrong;
instead of passing in two parameters I'm passing in a single array
that happens to have two elements.  Inside foo, parameter b would be
nil.  I need to somehow expand 'params' back from being a single
table/array to a number of separate parameters.

The best I can come up with is to use an anonymous "converter"
function, something like:

f = {
  func = function(p) foo (p[1], p[2]) end,
  params = {23, 42}

... but this requires me to know *at the time I write the code* how
many parameters the function takes.

Is there a prettier or more Lua-ish way?

Peter Pimley

PS:  If the example is a bit dry for you; I'm playing with Xavante and
I want to store all the parameters to wsapi.xavante.makeHandler inside
a single rule's 'params' value.