Erlang — How to convert a fun() object to a String

后端 未结 2 769
旧巷少年郎
旧巷少年郎 2020-12-14 02:47

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.

2条回答
  •  情深已故
    2020-12-14 03:24

    You might be able to use erlang:fun_info/2 for that, atleast i get some information from the shell when doing

    1> erlang:fun_info(fun() -> test,ok end, env).
    {env,[[],
         {value,#Fun},
         {eval,#Fun},
         [{clause,1,[],[],[{atom,1,test},{atom,1,ok}]}]]}
    2>
    

    You want the last list with the clause atom and then pretty print it using for instance erl_pp

提交回复
热议问题