18

Something like this:

function foo()
    print( __func__ )
   ...
end

How can it be done?

x-x
  • 7,287
  • 9
  • 51
  • 78
  • What are you trying to achieve? – lhf Oct 26 '10 at 09:11
  • 1
    See this post http://stackoverflow.com/questions/4021816/in-lua-how-can-you-print-the-name-of-the-current-function-like-the-c99-func/4037184#4037184 for what I'm trying to achieve :) – x-x Oct 28 '10 at 21:26

7 Answers7

31
#!/usr/bin/lua

local function myFunc()
 print(debug.getinfo(1, "n").name);
end
myFunc()
Ephraim
  • 767
  • 1
  • 8
  • 15
13

You can't. In lua, functions are values. So they don't have names. You might as well ask "what the name of 2" is. Just because some variable was assigned the value '2' doesn't make that variable the name of 2. Likewise "someFunc" is a variable - potentially one of many - that holds a particular function.


As per @Orouborus in the comments:

function f() end is just syntactical sugar for f = function () end

Whatever you are doing, whatever the other posts are helping you do, you can print out the name of the thing on the left, "f", but not the name of the thing on the right.

Chris Becke
  • 34,244
  • 12
  • 79
  • 148
  • how does your answer comply with the accepted answer? – phil294 Jul 26 '18 at 17:36
  • 4
    It doesn't. Both answers are technically correct. If - by name - you want the unique unchanging assigned name of a lua function - what __FUNC__ provides for C programs - you can't get it. If by name you simply want the name of an variable that has the function as its value, use the debug methods to get that, remembering that variables can be reassigned, and multiple variables can contain the same value - so the "name" returned this way is neither unique nor unchanging. – Chris Becke Aug 01 '18 at 06:19
  • 1
    In Lua, functions are _not_ variables. [They are _values_](https://www.lua.org/manual/5.4/manual.html#2.1) that can be passed around. – Roland Illig Sep 05 '20 at 09:15
  • 1
    First class citizen is not a valid reason to prevent access its name. For example, in Python, you can access it by `myFunc.__name__`. In JavaScript, it would be `myFunc.name`. But in C, after linking, there is nothing about function name for you. And we all know Python and JavaScript support first class function, but not C. – tsh Mar 03 '21 at 08:07
  • 1
    Afaik, `function f() end` is syntactical sugar for `f = function () end`. Lua functions don't have names. They can be assigned to variables, but the variable name isn't the function name. – Ouroborus Jan 16 '23 at 01:08
8
function __FILE__() return debug.getinfo(2, 'S').source end
function __LINE__() return debug.getinfo(2, 'l').currentline end
function __FUNC__() return debug.getinfo(2, 'n').name end

function printlinefilefunc()
    print("Line at "..__LINE__()..", FILE at "..__FILE__()..", in func: "..__FUNC__())
end

Output:

Line at 8, FILE at @./andydebug.lua, in func: printlinefilefunc

tom
  • 21,844
  • 6
  • 43
  • 36
Andy Yuan
  • 458
  • 5
  • 12
4

While I agree with Ephraim's answer, that code will not always report the same name as pointed out by Chris Becke. When the function is assigned to another variable, the "name" would be changed.

Here is another alternative. It just uses a string to identify the function. This method solves the changing name problem, but introduces a maintenance issue. The string would need to be kept in sync with the function name with future refactorization.

function foo()
  local __func__ = "foo"
  print( __func__ )
  --...
end

Alternatively, if the location of the function is more important than the name, the following may be better. It will give a name to the function that is based on the source and line number.

function getfunctionlocation()
  local w = debug.getinfo(2, "S")
  return w.short_src..":"..w.linedefined
end

function foo()
  print(getfunctionlocation()) --> foo.lua:6
  --...
end

If the __func__ still seems better, and standard Lua is not important, then the Lua parser can be modified as it is in this example for __FILE__ and __LINE__.

Community
  • 1
  • 1
gwell
  • 2,695
  • 20
  • 20
3

Use the Debug Library. It provides a getinfo(func) function that returns a table with information about the function.

schot
  • 10,958
  • 2
  • 46
  • 71
2

Functions don't necessarily have them. It's perfectly legal in Lua to create anonymous functions with no name- and call it, without assigning it one.

(function()
    print("in anonymous function!")
end)()

Is perfectly valid Lua. What name do you want to give that function?

Puppy
  • 144,682
  • 38
  • 256
  • 465
1

You can try:

local dbFunc = debug.getinfo(1) and debug.getinfo(1).name or ""

Info
Info["currentline"] = 1376
Info["source"] = "@.\mymod.lua"
Info["short_src"] = ".\mymod.lua"
Info["nups"] = 13
Info["isvararg"] = false
Info["what"] = "Lua"
Info["lastlinedefined"] = 1570
Info["func"] = function: 000000000030B440
Info["istailcall"] = false
Info["linedefined"] = 1375
Info["namewhat"] = "field"
Info["name"] = "ExportDB" <<--- See here the function name
Info["nparams"] = 4

debug.getinfo(1) Returns this structure of the current execution state of the active function in the stack.

If n is larger than the number of active functions in the stack, debug.getinfo() returns nil.

That's why you have to check if it exists before taking *.name and return an empty string if the information is not available

A it is called inside the ExportDB() function, it return its name