lua-users home
lua-l archive

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


On Mon, Dec 07, 1998 at 02:36:34PM -0200, Luiz Henrique de Figueiredo wrote:
> The "efficient" way in Lua is to *index* a table of
> functions. something like:

I've always thought it would be nice to be able to imbed context-less
code-blocks. Sort of like a run-time evaluated macro which would
execute in the context it was called from. This would allow the
table-driven-switch to operate without requiring you to pass in any
variables you wanted access to.

>  action={
> 	 [1] = function (x) B1 end,
> 	 [2] = function (x) B2 end,
> 	 ["nop"] = function (x) B3 end,
> 	 ["my name"] = function (x) B4 end,
>  }

With a little trickery, you can make this even cooler...

 Switch{
   [1] = function (x) B1 end,
   [2] = function (x) B2 end,
   ["nop"] = function (x) B3 end,
   ["my name"] = function (x) B4 end,
   ["__default__"] = function (x) B5 end
 }[x](arg);

Where the 'Switch' function sets the tag of the table so unknown
accesses return the "__default__" element.

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net