Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Lua, is there a function that given a function, it returns its name as a string?

Tags:

lua

Sorry if this is too obvious, but I am a total newcomer to lua, and I can't find it in the reference.

Is there a NAME_OF_FUNCTION function in Lua, that given a function gives me its name so that I can index a table with it? Reason I want this is that I want to do something like this:

local M = {}

local function export(...)
   for x in ...
     M[NAME_OF_FUNCTION(x)] = x
   end
end

local function fun1(...)
...
end

local function fun2(...)
...
end

.
.
.

export(fun1, fun2, ...)

return M
like image 456
Paralife Avatar asked Oct 25 '11 08:10

Paralife


People also ask

How do you name a function in Lua?

You can't. In lua, functions are first class variables. So they don't have names.

Is return a function Lua?

The Lua return statement is used to return result from a function or to finish a function. The return statement is a built in statement or keyword in the Lua programming. The return statement is a special statement which is used inside a method or function to send the functions's result back to the function caller.

What is an anonymous function Lua?

Lua Functions Anonymous functions Anonymous functions are just like regular Lua functions, except they do not have a name. doThrice(function() print("Hello!") end) As you can see, the function is not assigned to any name like print or add . To create an anonymous function, all you have to do is omit the name.


2 Answers

There simply is no such function. I guess there is no such function, as functions are first class citizens. So a function is just a value like any other, referenced to by variable. Hence the NAME_OF_FUNCTION function wouldn't be very useful, as the same function can have many variable pointing to it, or none.

You could emulate one for global functions, or functions in a table by looping through the table (arbitrary or _G), checking if the value equals x. If so you have found the function name.

a=function() print"fun a" end
b=function() print"fun b" end
t={
   a=a,
   c=b
}
function NameOfFunctionIn(fun,t) --returns the name of a function pointed to by fun in table t
   for k,v in pairs(t) do
       if v==fun then return k end
   end
end
print(NameOfFunctionIn(a,t)) -- prints a, in t
print(NameOfFunctionIn(b,t)) -- prints c
print(NameOfFunctionIn(b,_G)) -- prints b, because b in the global table is b. Kind of a NOOP here really.

Another approach would be to wrap functions in a table, and have a metatable set up that calls the function, like this:

fun1={
    fun=function(self,...)
        print("Hello from "..self.name)
        print("Arguments received:")
        for k,v in pairs{...} do print(k,v) end
    end,
    name="fun1"
}
fun_mt={
    __call=function(t,...)
        t.fun(t,...)
    end,
    __tostring=function(t)
        return t.name
    end
}
setmetatable(fun1,fun_mt)
fun1('foo')
print(fun1) -- or print(tostring(fun1))

This will be a bit slower than using bare functions because of the metatable lookup. And it will not prevent anyone from changing the name of the function in the state, changing the name of the function in the table containing it, changing the function, etc etc, so it's not tamper proof. You could also strip the tables of just by indexing like fun1.fun which might be good if you export it as a module, but you loose the naming and other tricks you could put into the metatable.

like image 182
jpjacobs Avatar answered Sep 22 '22 05:09

jpjacobs


Technically this is possible, here's an implementation of the export() function:

function export(...)
        local env = getfenv(2);
        local funcs = {...};
        for i=1, select("#", ...) do
                local func = funcs[i];
                for local_index = 1, math.huge do
                        local local_name, local_value = debug.getlocal(2, local_index);
                        if not local_name then
                                break;
                        end
                        if local_value == func then
                                env[local_name] = local_value;
                                break;
                        end
                end
        end
        return env;
end

It uses the debug API, would require some changes for Lua 5.2, and finally I don't necessarily endorse it as a good way to write modules, I'm just answering the question quite literally.

like image 28
MattJ Avatar answered Sep 22 '22 05:09

MattJ