Call a local function by name.

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Call a local function by name.

Laurent FAILLIE
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

Reply | Threaded
Open this post in threaded view
|

Re: Call a local function by name.

Mark Gabby

Someone correct nee if I'm wrong, but you can't, unfortunately. Local functions and variables are special in Lua. They're stored as numerical IDs internally.

You could use debug.getlocal to iterate over all locals, look for a matching name, and call its value, but that would be rather awkward. Though you could wrap it in an (inefficient) function.

On May 3, 2013 8:14 AM, "Laurent Faillie" <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: Call a local function by name.

Laurent FAILLIE
Le 03/05/2013 17:27, Mark Gabby a écrit :
>
> Someone correct nee if I'm wrong, but you can't, unfortunately. Local
> functions and variables are special in Lua. They're stored as
> numerical IDs internally.
>
> You could use debug.getlocal to iterate over all locals, look for a
> matching name, and call its value, but that would be rather awkward.
> Though you could wrap it in an (inefficient) function.
>
In this case, I would prefer to stay with my current loadstring()
solution ...


Reply | Threaded
Open this post in threaded view
|

Re: Call a local function by name.

Mark Gabby

>> In this case, I would prefer to stay with my current loadstring() solution ...

You can store a local function in any variable. I mean, you could even create a local function and make it global by setting a name in global scope to its value.

You can even make your own table with functions that are "local" to it in the sense that they can only be accessed through the table.

Why do you want a "local" function?

Reply | Threaded
Open this post in threaded view
|

Re: Call a local function by name.

Roberto Ierusalimschy
In reply to this post by Mark Gabby
> Someone correct nee if I'm wrong, but you can't, unfortunately. Local
> functions and variables are special in Lua. They're stored as numerical IDs
> internally.

My only correction is that I would use the adjective "special" the other
way around. Usually, variable names (as other identifiers in programming
languages) serve only to connect uses of something with its definition,
so it is quite usual for a compiler or precompiler to convert them to
numbers, addresses, etc. In particular, renaming a variable to another
name (of course avoiding collisions) should never change the meaning of a
program.

What is special is global variables (in the case of Lua), which allows a
form of introspection.

-- Roberto


Reply | Threaded
Open this post in threaded view
|

Re: Call a local function by name.

Laurent FAILLIE
In reply to this post by Mark Gabby
Le 03/05/2013 17:44, Mark Gabby a écrit :
>
> >> In this case, I would prefer to stay with my current loadstring()
> solution ...
>
Arg : I got this code from http://stackoverflow.com

_a = loadstring('return '.. func_name ..'(...)')(arg)

but it doesn't suit my need either : it's looking only for global
functions :(
>
> You can even make your own table with functions that are "local" to it
> in the sense that they can only be accessed through the table.
>
It's what I will do : creating a local dictionary table of all my local
functions.
>
> Why do you want a "local" function?
>
Because I don't want to pollute my global area : they are used in
plug-ins for a larger system and I would like to avoid any interference
b/w each plug-in.
I'm have to finish some small thing, made a release and will present it
to the list (this list was my major source of information for part of it
:) ).

Bye

Reply | Threaded
Open this post in threaded view
|

Re: Call a local function by name.

Elias Barrionovo
In reply to this post by Laurent FAILLIE
On Fri, May 3, 2013 at 12:14 PM, Laurent Faillie <[hidden email]> 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 ?
>

Can't you just store these functions inside a table instead of in
local variables?

local named_funcs = {}
named_funcs.normMilli = function ( v ) return v/1000 end
local name = 'normMili'
print(named_funcs[name](5e3))

--
NI!

() - www.asciiribbon.org
/\ - ascii ribbon campaign against html e-mail and proprietary attachments

Reply | Threaded
Open this post in threaded view
|

Re: Call a local function by name.

David Given
In reply to this post by Laurent FAILLIE
Laurent Faillie wrote:
[...]
> Because I don't want to pollute my global area : they are used in
> plug-ins for a larger system and I would like to avoid any interference
> b/w each plug-in.

You can always do something like this:

local function func1() ... end
local function func2() ... end
local function func3() ... end

local functable = {
        ["func1"] = func1,
        ["func2"] = func2,
        ["func3"] = func3,
}

local function call_by_name(s)
        local f = functable[s]
        f()
end

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

│ 𝕻𝖍'𝖓𝖌𝖑𝖚𝖎 𝖒𝖌𝖑𝖜'𝖓𝖆𝖋𝖍 𝕮𝖙𝖍𝖚𝖑𝖍𝖚 𝕽'𝖑𝖞𝖊𝖍
𝖜𝖌𝖆𝖍'𝖓𝖆𝖌𝖑 𝖋𝖍𝖙𝖆𝖌𝖓.



signature.asc (271 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Call a local function by name.

Dirk Laurie-2
2013/5/3 David Given <[hidden email]>:

> local functable = {
>         ["func1"] = func1,
>         ["func2"] = func2,
>         ["func3"] = func3,
> }

Or

local functable = {
         func1 = func1,
         func2 = func2,
         func3 = func3,
 }

Reply | Threaded
Open this post in threaded view
|

Re: Call a local function by name.

Rena
..
On Fri, May 3, 2013 at 12:24 PM, Dirk Laurie <[hidden email]> wrote:
2013/5/3 David Given <[hidden email]>:

> local functable = {
>         ["func1"] = func1,
>         ["func2"] = func2,
>         ["func3"] = func3,
> }

Or

local functable = {
         func1 = func1,
         func2 = func2,
         func3 = func3,
 }


Or if your functions are small enough, even:

local functable = {
         func1 = function(arg1, arg2) return arg1 + arg2 end,
         func2 = function(arg1, arg2) return arg1 - arg2 end,
         func3 = function(arg1, arg2) return arg2 - arg1 end,
 }

The only difference between a local and a global function is that a global function can be found in _G. Whether it got there by writing "function myfunc(...)" or by "_G.myfunc = myfunc" or "function _G.myfunc(...)" or "_G.myfunc = function(...)" makes no difference.

--
Sent from my Game Boy.
Reply | Threaded
Open this post in threaded view
|

Re: Call a local function by name.

Tim Hill
In reply to this post by Laurent FAILLIE
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 <[hidden email]> 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
>