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?


defmodule Producer do
  def special_list do

defmodule Consumer do
  @special_attribute Producer.special_list()
1 Like

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.


Thanks very much!

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: