Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Lua, how can you print the name of the current function, like the C99 __func__ identifier?

Tags:

lua

Something like this:

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

How can it be done?

like image 919
x-x Avatar asked Oct 26 '10 08:10

x-x


People also ask

How do I print a function name in Lua?

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

Is print a function in Lua?

Lua print in Lua programming language is an inbuilt function that is used for printing the required statement in the console.

How many functions does Lua have?

This means that all values can be stored in variables, passed as arguments to other functions, and returned as results. There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.


1 Answers

#!/usr/bin/lua

local function myFunc()
 print(debug.getinfo(1, "n").name);
end
myFunc()
like image 144
Ephraim Avatar answered Sep 28 '22 11:09

Ephraim