Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6746919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:22:46+00:00 2026-05-26T12:22:46+00:00

Sorry if this is too obvious, but I am a total newcomer to lua,

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:22:47+00:00Added an answer on May 26, 2026 at 12:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry if this is too obvious but I can't find any proper answer
Sorry if this is too simple, but I simply couldn't find a tutorial nor
Sorry if this is too simple, but thanks in advance for helping. This is
Sorry if this question is too vague, but I'd rather not muddy it's point
I am sorry if this is a duplicate or too elementary, but how do
This is going to sound too silly / too basic - sorry about that,
Sorry for the wall of text guys but this one requires expliaining, way too
Sorry if this too open ended for this forum, but here goes. I mostly
Sorry if this is too simple, but I've found a lot of documentation on
Good afternoon, I'm sorry if this question is too silly, but I don't know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.