Anonymous functions as Module attributes?

1,011 views
Skip to first unread message

Sean Stavropoulos

unread,
Feb 17, 2015, 7:10:34 PM2/17/15
to elixir-l...@googlegroups.com
I'm just getting started with my Elixir Macro learning, so pardon my lack of understanding.

defmodule AnonymousFunctionTest do
 
@anon fn() -> true end

 
def anonymous_function,
   
do: @anon
end


** (ArgumentError) cannot inject attribute @anon into function/macro because cannot escape #Function<0.35353592 in file:iex>. The supported values are: lists, tuples, maps, atoms, numbers, bitstrings, pids and remote functions in the format &Mod.fun/arity

Is there a particular reason why implementing anonymous functions  isn't technically feasible?  The reason I ask is because I have a particular scenario in mind where using an anonymous function in a macro could provide for a nicer user experience.

Eric Meadows-Jönsson

unread,
Feb 18, 2015, 2:42:52 AM2/18/15
to elixir-l...@googlegroups.com
It's not allowed because anonymous functions does not survive compilation. A closure "closes" over an environment, the environment you have during compilation is not available after compilation finished.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/ebfb4a3f-23b8-41a3-9412-d2707e477c8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Eric Meadows-Jönsson

Eric Meadows-Jönsson

unread,
Feb 18, 2015, 2:44:41 AM2/18/15
to elixir-l...@googlegroups.com
There are workaround if you are using macros because you can store the quoted expression of an anonymous function.
--
Eric Meadows-Jönsson
Reply all
Reply to author
Forward
0 new messages