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

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

Something like this:

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

How can it be done?

7条回答
  •  死守一世寂寞
    2021-02-13 04:10

    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?

提交回复
热议问题