lua-users home
lua-l archive

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


Functions don't have names in Lua, just the variables that store them (actually, store references to them).

And global variables are actually implemented as a distinguished table, _G, and since it's a table you get the luxury of accessing that table via string names: _G["some global"], hence the ability to call access globals indirectly via strings.

Locals are NOT implemented as Lua tables, so you don't get the same string based indirect access; you can only access a local variable directly by its name.

So you have a couple of choices:

1. Store the function in a global, or in a private table.

2. Store the original function in a local, then copy it to a global as necessary. You code below would then be:

local name = normMilli
-- Note the lack of quotes!

I would guess this is really what you want: to store references to the function, not the name (it's also a lot more efficient).
--Tim

On May 3, 2013, at 8:14 AM, Laurent Faillie <l_faillie@yahoo.com> wrote:

> Hello,
> 
> How to call a local function by it's name ?
> 
> Something like
> 
> ----
> local function normMilli( v ) return v/1000 end
> 
> local name = 'normMilli'
> ----
> Then I need the call the function for which the name is stored in name
> variable.
> 
> I know for global function, I can use _G, but I didn't find for local ones ?
> 
> Thanks
> 
> Laurent
>