Cannot compile attribute

I am trying to compile a simple module where I get a module from the config and save it on an attribute.

defmodule Test do
  @metrics                  Application.get_env(:my_app, :metrics_module)
  @default_deps %{
    count_error:  &@metrics.inc/1
  }
end

However I cannot even compile the code for I have the following error:

== Compilation error in file lib/worker/worker.ex ==
** (ArgumentError) cannot inject attribute @default_deps into function/macro because cannot escape #Function<0.55850841 in file:lib/worker/worker.ex>. The supported values are: lists, tuples, maps, atoms, numbers, bitstrings, PIDs and remote functions in the format &Mod.fun/arity
    (elixir) lib/kernel.ex:2879: Kernel.do_at/5
    (elixir) expanding macro: Kernel.@/1
    lib/worker/worker.ex:47: Cooper.Worker.find/1
    (elixir) lib/kernel/parallel_compiler.ex:208: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

Why can’t this compile given that my dev.exs does have that parameter defined?

Elixir does not allow anonymous functions as module attributes. Here is an explicit test for that.

You simply can not put anonymous function into a module attribute. This is a limitation of the language.

Try saving it as a MF-tuple.

You’ll need extra work to call it then though.

Something like this:

@def_deps %{ count_error: {@metrics, :inc} }

def dosomething() do
  {m, f} = @def_deps.count_error
  apply(m, f, [:dosomething])
end