dcdourado

dcdourado

Module dependency (?) - Redefinition of attributes

When a module attribute is based on the return of a function defined on another module, can we say we’ve a module dependency? Module A defines and exposes a module-attribute through a public function and Module B defines another module-attribute based on the return of that function.

This affirmation (module dependency) makes sense to me because module attributes are defined on compile time, and because of that Module B (which contains a module-attribute that depends on the definition of Module A) now has a dependency of Module A being before. Is this the correct concept?

Anyway, the real problem here is the redefinition of modules on this scenario. I’ve made a quick demo that shows that a redefinition on the module that possess the module-attribute won’t take any effect on the other module.

Is there a hook like @before_compile which can help me here? The main goal is to define some values and make Module A the single source of truth of these values, which will be used as guards, changeset validations and features based on this business-rule.

Most Liked

OvermindDL1

OvermindDL1

So I cloned the demo, I ran mix deps.get && mix compile and then loaded it in iex via iex -S mix, I run:

iex(1)> {ModuleDependency.ModuleA.values, ModuleDependency.ModuleB.values}
{[:value_a, :value_b, :value_c], [:value_a, :value_b, :value_c]}

And they match, so then I edit the values in ModuleA by commenting the old values and uncommenting the commented new values, I then run this in that same iex session:

iex(2)> recompile                                                         
Compiling 2 files (.ex)
:ok
iex(3)> {ModuleDependency.ModuleA.values, ModuleDependency.ModuleB.values}
{[1, 2, 3], [1, 2, 3]}

And it worked fine here? You might need to give more details on what you are doing. Remember that module dependencies are handed by mix/compiler (pasting things in the REPL does not use mix/elixirc, calling recompile, which is just an import for IEx.Helpers.recompile/0 however does, mix/elixirc works on files, not interpreted code put in via the REPL in very short summary).

dcdourado

dcdourado

Thanks for the response @OvermindDL1!

On production level I can’t change the code and recompile it because of the difficulties of doing it on a running docker image and I’m afraid calling recompile command could harm a production-running application.

Given a scenario where a quick fix is required, pasting a module with changes on console may best than a full-deploy on terms of speed. Is this a legitimate worry? Should I be concerned about this?

OvermindDL1

OvermindDL1

Pasting code into the REPL will not perform any compiler-related cross-file work. The REPL is for interacting with a running system, not for compiling projects. ^.^

But what difficulties are you having with it in a docker container, I’ve had no issues here?

But still, when you’ve automated a full deploy with hot code swapping then it’s really really fast anyway (don’t even need to restart a docker container, hot code swapping is very nice!). :slight_smile:

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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 30877 112
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
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
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
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
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement