Inspecting anonymous functions

211 views
Skip to first unread message

Tallak Tveide

unread,
Jul 13, 2014, 6:50:57 PM7/13/14
to elixir-l...@googlegroups.com
Using anonymous functions for me is nice, but I do find it har to debug when I cannot see what they contain. That's why I'm thinking it would be nice to have an inspect function for anonymous functions that could print out the function and any bound data, eg:

    iex(2)> f = fn x -> fn -> x + 10 end end
    #Function<6.106461118/1 in :erl_eval.expr/5>
    iex(3)> Function.inspect f
    # function at file ffff line llll
    fn x ->
      fn ->
        x + 10
      end
    end
    iex(4)> Function.inspect f.(10)
    # function at file ffff line llll
    fn ->
      10 + 10
    end



Would this be possible to implement somehow? Right now there doesn't seem to be an interface into a function value... Macros won't work as this would happen at runtime I guess..

José Valim

unread,
Jul 14, 2014, 5:44:31 AM7/14/14
to elixir-l...@googlegroups.com
Unfortunately we can't do that right now.

I know Erlang stores its abstract format in functions [1] and maybe we can do the same once we change our compiler to compile to core erlang (instead of erlang abstract format) but this change is not happening in the short term because it breaks things like dialyzer.

[1] Try in your iex shell: :erlang.fun_info(fn x, y -> x + y end)



José Valim
Skype: jv.ptec
Founder and Lead Developer


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Virding

unread,
Jul 14, 2014, 7:30:29 AM7/14/14
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
This only works when the fun is defined in the shell, i.e. when it is interpreted. Funs from modules don't contain the abstract code, at least not so :erlang.fun_info gets it.

The reason it works with interpreted code is a result of how funs are implemented in erl_eval, the erlang interpreter. It uses internal funs in erl_eval and the abstract code is passed as data into those funs which is then interpreted when the fun is called. A nice hack.

Robert

José Valim

unread,
Jul 14, 2014, 7:48:36 AM7/14/14
to elixir-l...@googlegroups.com
Ah, nice to know, thanks Robert!



José Valim
Skype: jv.ptec
Founder and Lead Developer


Reply all
Reply to author
Forward
0 new messages