Take the tour ×  Is there a straightforward way to convert an
Erlang fun to a string? A call to io_lib:format only prints the
function reference, e.g. something like
"#Fun<erl_eval.20.67289768>". For example, I'd like to be able
to do this:  1> Fun = fun() -> atom_to_list('hello world') end.
2> FunStr = fun_to_str(Fun). "fun() -> atom_to_list('hello
world') end."  I'm looking for how to implement fun_to_str. In
javascript, some interpreters have a .toSource() function that
can be called on any object, including functions, that print
their string representation. Any info is appreciated, thanks.
share|improve this question add comment  2 Answers  up vote 15
down vote accepted  First, get the environment variables for the
fun (which includes the abstract code):  1> {env, Env} =
erlang:fun_info(Fun, env).
[{atom,4,'hello world'}]}]}]]}  The last element in the list is
the abstract code:  2> Abs = lists:last(Env).
[