lua-users home
lua-l archive

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


I think the most elegant option for your problem is in place function
closures.
For instance...

local myTable =

{

            {?hello?, nil, function () do return myFunc (3,4) end},

            {?not now?', nil, function () do return print(?the end is nigh?)
end},

            {?maybe later?, nil, function () do return anotherFunc(5,6) end}

}



This forms a function that takes no parameters, but when called performs
whatever is needed (such as calling the other functions with the appropriate
parameters).



If wrapping your existing function calls is too tedious an not "pretty"
enough you can fall back on your table construct, though adding an item to
each sub-table would simplify/beautify things IMHO.


For instance...

local myTable =

{

            {?hello', nil, myFunc, { 3,4 } },

            {?not now?', nil, print, {?the end is nigh?} },

            ?

}



The big advantage here is that passing the parameters to the function
becomes simply...



myTable[1][3](unpack(myTable[1][4]))

  -----Original Message-----
  From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of O'Riain, Colm
  Sent: Tuesday, June 22, 2004 11:56 AM
  To: lua@bazar2.conectiva.com.br
  Subject: Suppress Function Evaluation


  My apologies if this has been covered recently on this list but I couldn?t
find it in the archive.



  I?m trying to pass a table containing functions as a parameter as follows:



  local myTable =

  {

              {?hello?, nil, myFunc (3,4) },

              {?not now?', nil, print(?the end is nigh?) },

              {?maybe later?, nil, anotherFunc(5,6) }

  }

  bigFunc(myTable);



  where myFunc and anotherFunc are pre-declared functions and bigFunc is a
function which _might_ call the 3rd entry in each subTable in myTable. The
problem is that on calling bigFunc, the 3rd parameter of each subTable is
always evaluated and the function is always called immediately, which is not
desirable. Is there a way to suppress evaluation at call time or am I
looking at this assways?



  I?ve got around it by passing the function and its parameters in as a
table



  local myTable =

  {

              {?hello', nil, {myFunc,3,4 },

              {?not now?', nil, {print,?the end is nigh?} },

              ?

  }

  bigFunc(myTable);



  and then putting the function call together inside bigFunc but this feels
like cheating given how flexible Lua is.



  Thanks for any light you can throw on this



  Colm

<<attachment: winmail.dat>>