joey_the_snake

joey_the_snake

Setting a module attribute from a function in a different module

I have 2 modules, one of them has a function that returns a list and the other one is using that function to create a module attribute.

Is this a safe operation in Elixir? I’m not sure if it’s guaranteed that the first module will be compiled before the second one. Also, does it matter if the modules are in the same file/different files?

e.g:

defmodule Producer do
  def special_list do
    [1,2,3]
  end
end

defmodule Consumer do
  @special_attribute Producer.special_list()
end

Most Liked

kip

kip

ex_cldr Core Team

Welcome to the forum!

The compiler will detect that you are calling a remote function on the Producer module and will compile the Producer module if it needs to. So yes, it’s a safe thing to do and quite a common pattern.

szajbus

szajbus

Calling a function from another module at compile-time creates a compile-time dependency to that module.

If a Producer also had compile-time dependency to Consumer (direct or indirect), compiler would run in a deadlock.

So, it’s possible, but beware :slight_smile:

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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