lua-users home
lua-l archive

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


----- Original Message -----
> oh, one other quick question, is it possible to pass a function
> as a parameter to another function?  I havn't figured out how
> to do this.  I want to do a call-back type setup:

Yes, you can do this, pretty much exactly as you described.. A function can
be passed as a parameter to a function just like anything else, and called
however you want. Here's a really lame and contrived example of how you
could pull this off:

-- Snip --
function doSomethingWithFunc (func)
 func ("Hello")
end

doSomethingWithFunc (print)
-- Snip --

Of course, running this causes Hello to be printed. You can expand the
concept as needed, with more parameters, etc. The function in question can
of course even be a C function and not a lua one.

The only downside (or upside, depending on your outlook) is that there's no
way to enforce a particular function signature. So you have to use self
discipline to make sure that all functions used as callbacks have the right
parameters in the right order.