Where is the error

Hi all

When I try to alias a module does not exist, why I do not get an error?

iex(2)> alias Does.Not.Exist



My guess is that since it is not possible to know for sure if the given module really exists without invoking it, like knowing if a module has been compiled or loaded (without enforcing a check), the alias just sets up, well, the alias and doesn’t check if the module actually exists, since the module is just being passed as an argument and not being actually invoked.

But don’t quote me on this. This is just my guess from my limited understanding of Elixir’s compiler and internals.

Alias only used to define shortcuts in typing only.

If you try calling functions inside it (which is not exist), it will give an exception-

iex(1)> alias Does.Not.Exist
iex(2)> Exist.good
** (UndefinedFunctionError) function Does.Not.Exist.good/0 is undefined (module Does.Not.Exist is not available)


In Elixir (BEAM in general), there is no “does not exist”, there is only an “can’t find it right now”.

And just for knowing the name of a module, you do neither check if it is there or if it does contain any functions.

It is a little bit like writing a letter with instructions and sending it.

You can prepare thousand of envelops with non-existing addresses, none of the envelopes will explode in your face. But when you actually take them to the postoffice, they will happily try to deliver each one of them.

A couple of days later all the magic happens, you will get different types of response:

  1. The recipient of the letter tells you that he has finished his task succesfully (Module and function do exist and params were valid)
  2. The recipient tells you that he did not understand your request (Module and function do exist and params were vinalid)
  3. The recipient tells you that he did not know what to do (Module exists but function unknown)
  4. You get your original envelope back, unopened with a note that the recipient is unknown (Module not available)

But remember, even if the recipient was unknown today, next year there could be a house with someone living inside (Module can get available later and then be loaded without problems).

1 Like

If we break this down Does.Not.Exist is really an atom (check is_atom(Does.Not.Exist)) and all alias does is setup Exist to expand to Does.Not.Exist. So, once you have setup the alias you can try running Does.Not.Exist == Exist and it will return true. What I am trying to say is modules are just atoms both in erlang and elixir and alias just sets up a shortcut for the atom.

However, if you want to find code which is unreachable like this, you can use the new mix xref task. It’ll show you code like this.