[erlang-questions] Finding a way to get function name from local function reference

Richard Carlsson <>
Wed Jan 21 09:39:41 CET 2009


Sergey S wrote:
> What I'm looking for is the way to get readable function name at runtime.
> 
> Here is pseudo code:
> 
> get_name(fun some_mod:some_fun/0) -> some_fun
> get_name(fun some_fun/0)                  -> some_fun
> 
> I took a look at "erl -man erlang" to find out what I can use in this
> case. erlang:fun_info(Fun, name) seems useful but it works only for
> exported function references:
> 
> % erlang:fun_info/2 will return the readable name: {name, some_fun}.
> That is what I want to get.
> erlang:fun_info(fun some_module:some_fun/0, name)
> 
> But it doesn't work for local function references (I've checked):
> 
> % Note that 'some_module' is omitted. This function will return
> something like {name,'-test/0-fun-0-'} which is incomprehensible to me
> erlang:fun_info(fun some_fun/0, name).
> 
> With local function references I have to use something other... Is
> there a simple way to get the name of the function?

This is due to a compiler implementation detail (broken, imho), where
a local function reference "fun foo/N" is actually compiled by rewriting
it as "fun (...) -> foo(...) end", which means that the name you get
from fun_info() is the generated internal name for the new anonymous
function. The name "foo" is hidden and there is nothing you can do
to get hold of it. It would be nice if the compiler was changed to
generate direct references instead of wrapping them like this.

    /Richard



More information about the erlang-questions mailing list