michel.boaventura

michel.boaventura

I’m trying to understand why Elixir creates a runtime dependency when we use a module on a function clause.

For instance:

# lib/a.ex
defmodule A do
end

# lib/b.ex
defmodule B do
  def foo(A) do
    IO.inspect("A")
  end
end

Here module B has a dependency on A even though we aren’t using anything related to the module itself, it just so happens that A is an atom and also a module. If it was just an atom everything would work the same way and we (obviously) wouldn’t have a dependency.

First 2 of 2 Posts Switch mode

stefanchrobot

stefanchrobot

I don’t know the answer, but if this is an issue for you, maybe you can use the Phoenix.Router’s approach to this:

It is very common in Phoenix applications to namespace all of your routes under the application scope:

scope "/", MyAppWeb do
 get "/pages/:id", PageController, :show
end

The route above will dispatch to MyAppWeb.PageController. This syntax is not only convenient for developers, since we don’t have to repeat the MyAppWeb. prefix on all routes, but it also allows Phoenix to put less pressure on the Elixir compiler. If instead we had written:

get "/pages/:id", MyAppWeb.PageController, :show

The Elixir compiler would infer that the router depends directly on MyAppWeb.PageController, which is not true. By using scopes, Phoenix can properly hint to the Elixir compiler the controller is not an actual dependency of the router. This provides more efficient compilation times.

michel.boaventura

michel.boaventura OP

I’m using Plug alone so I don’t have the scope macro, but I ended up cooking something similar to solve the issue, but couldn’t find out why this kind of dependency is needed. Thanks for the reply!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement