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

前端 未结 7 1754
别那么骄傲
别那么骄傲 2021-02-13 03:49

Something like this:

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

How can it be done?

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-13 04:28

    You can't. In lua, functions are first class variables. 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.

提交回复
热议问题