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

后端 未结 2 763
旧巷少年郎
旧巷少年郎 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<shell.7.37281544>},
         {eval,#Fun<shell.24.85590193>},
         [{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

    0 讨论(0)
  • 2020-12-14 03:31

    First, get the environment variables for the fun (which includes the abstract code):

    1> {env, [{_, _, _, Abs}]} = erlang:fun_info(Fun, env).                     
    {env,[{[],
           {eval,#Fun<shell.21.83096281>},
           {value,#Fun<shell.5.83096281>},
           [{clause,1,[],[],
                    [{call,1,{atom,1,atom_to_list},[{atom,1,hello_world}]}]}]}]}
    

    Pretty print the abstract code using erl_pp:

    3> Str = erl_pp:expr({'fun', 1, {clauses, Abs}}).           
    [[[["fun",
        [[[[["()"]," ->"],
           ["\n       ",
            [["atom_to_list",[[40,["'hello world'",41]]]]]]]]]]],
      [10,["end"]]]]
    4> io:format([Str|"\n"]).
    fun() ->
           atom_to_list('hello world')
    end
    ok
    

    (You have to add {'fun', 1, {clauses, ...}} around it to make it a complete Erlang expression)

    0 讨论(0)
提交回复
热议问题