# Dynamic modules / module "aliasing"

**URL:** <https://forum.elixirforum.com/t/dynamic-modules-module-aliasing/50737>\
**Category:** Questions\
**Tags:** macros, development\
**Created:** [October 3, 2022, 9:18pm UTC](https://forum.elixirforum.com/t/dynamic-modules-module-aliasing/50737 "2022-10-03T21:18:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhpip](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nhpip/32/23811_2.png) [@nhpip](https://forum.elixirforum.com/u/nhpip)\
**Post date:** [October 3, 2022, 9:18pm UTC](https://forum.elixirforum.com/t/dynamic-modules-module-aliasing/50737/1 "2022-10-03T21:18:32Z")

</div>

Hey,

I know there are other ways to do this, but I went down a deep rabbit hole last night trying to figure this one out 🙂 … this is purely a though experiment.

Say you have 2 almost identical dependencies, but you only want to use one of them at a time. Maybe the choice of dependency is geographic based?

Both dependencies have an example module hierarchy like:

```elixir
 M0
  |
  M0.M1 - M0.M2 - M0.M3
            |
         M0.M2.M4

```

Now of course a simple solution to ensure the code in the rest of the application selects the correct dependency would be something like this in all the modules that use it:

```elixir
  config = Application.compile_env(:my_app, SpecialDependency, [])
  @dependency Keyword.get(config, :special_dep)

  # And code would do:
  @dependency.foo(a, b, c)

```

Seems a bit hokey…what would be nice is to create a new (set) of stub modules that, for want of a better word “_alias_” the correct dependency. All the application code would call the stub which would get mapped onto the correct target dependency.

For example (yes, I can also use `defdelegate`)

```elixir
defmodule SpecialDependencyStub do

  config = Application.compile_env(:my_app, SpecialDependency, [])
  @dependency Keyword.get(config, :special_dep)

 # Generate the same functions as the real dependency
  Enum.each(@dependency. __info__ (:functions), fn({fun, arity}) ->
     args = Macro.generate_arguments(arity, __MODULE__ )
     def unquote(fun)(unquote_splicing(args)), do: unquote(@dependency).unquote(fun)(unquote_splicing(args))
  end)
end

```

Now that works fine…where I am stumped is how to handle all the modules under the `SpecialDependency`. I want those modules to retain the same name as the real dependency, and I’d rather not have to repeat the above for every module. Something like

```elixir
Enum.each(modules, fn(mod) -> WizzardsBeHere end)

```

Hopefully that all makes sense

---

<div class="post-metadata">

**Author:** ![hst337](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/h/41988e/32.png) [@hst337](https://forum.elixirforum.com/u/hst337)\
**Post date:** [October 3, 2022, 10:06pm UTC](https://forum.elixirforum.com/t/dynamic-modules-module-aliasing/50737/2 "2022-10-03T22:06:43Z")

</div>

Why would you want to write `Stub.foo(bar)` instead of `@stub.foo(bar)`?

---

<div class="post-metadata">

**Author:** ![felix-starman](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/felix-starman/32/18051_2.png) [@felix-starman](https://forum.elixirforum.com/u/felix-starman)\
**Post date:** [October 3, 2022, 11:31pm UTC](https://forum.elixirforum.com/t/dynamic-modules-module-aliasing/50737/3 "2022-10-03T23:31:53Z")

</div>

I am slowly working on a core rewrite of the Double library for this purpose, so it can be used as a drop-in for Mox and allow folks the tools to slowly introduce behaviours and mocks

The code is dense and in the middle of a refactor, but if you’re interested, I’m taking a somewhat similar approach, at least in the defmock side.

[https://github.com/petermueller/double/blob/add-defmock/lib/double.ex](https://github.com/petermueller/double/blob/add-defmock/lib/double.ex)

---

<div class="post-metadata">

**Author:** ![nhpip](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nhpip/32/23811_2.png) [@nhpip](https://forum.elixirforum.com/u/nhpip)\
**Post date:** [October 4, 2022, 12:56am UTC](https://forum.elixirforum.com/t/dynamic-modules-module-aliasing/50737/4 "2022-10-04T00:56:07Z")

</div>

I’ll take a look…thanks

---

<div class="post-metadata">

**Author:** ![Hermanverschooten](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hermanverschooten/32/6447_2.png) [@Hermanverschooten](https://forum.elixirforum.com/u/Hermanverschooten)\
**Post date:** [October 10, 2022, 6:26am UTC](https://forum.elixirforum.com/t/dynamic-modules-module-aliasing/50737/5 "2022-10-10T06:26:02Z")

</div>

If these modules have the same interface, couldn’t you use a macro that would `alias` the correct module and you’d use that alias in your code?
