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

New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement