我可以在运行时找到当前函数的名称吗?
foo() ->
foo = find_function_name().
Run Code Online (Sandbox Code Playgroud)
是否可以编写有趣的find_function_name/0?你会怎么做?它已经存在了吗?
更轻量级,也不依赖于偶尔会改变的堆栈跟踪格式,我宁愿使用process_info/2:
{_, {Module, Function, Arity}} = process_info(self(), current_function)
Run Code Online (Sandbox Code Playgroud)
在其中,Function您将找到作为原子的函数名称,并且您会得到 theModule和 the ArityAlso。您不能将其编写为函数,因为它只会将此函数作为当前函数返回。为您提供当前函数名称作为原子的宏可能如下所示:
-define(current_function_name(),
element(2, element(2, process_info(self(), current_function)))).
foo() ->
foo = ?current_function_name().
Run Code Online (Sandbox Code Playgroud)