lua-users home
lua-l archive

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


Quoth Grizzly Bear <6grizzlybear@gmail.com>, on 2013-01-04 17:11:29 -0800:
(rearranged)
> >> I'd like to have a meta function, sth. like
> >>
> >>         function fn_matrix(fn_name, arg_foo)
> >>                 string2var(fn_name) = function(arg_foo) print(fn_name,
> >> arg_foo) end
> >>         end
> > Wouldn't you be able to replace string2var(fn_name) with _G[fn_name] to
> > create a global function of the specified name?
> >
> That's cool. Thanks!

But it might be easier, more composable, and more obvious that a
global is being defined if fn_matrix were to return the new function
instead.  Then you'd do:

  new_fun = fn_matrix(arg_foo)
  new_fun(...)

Note that these are already equivalent:

  function f(...) ... end
  f = function(...) ... end

(Though if you have another reason to use the _name_ of the desired
function in its construction, then you wind up having to pass the name
in as well, thus specifying it twice, which is not so good---in that
case, the tradeoff may be different.)

   ---> Drake Wilson