Runtime dependency between modules cause by function definition

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

# lib/b.ex
defmodule B do
  def foo(A) do

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.

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

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.

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!

1 Like