Fl4m3Ph03n1x

Fl4m3Ph03n1x

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?

Marked As Solved

NobbZ

NobbZ

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

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement