Search code examples
erlangelixir

In Elixir, why is "alias" preferred over "import" for importing the modules?


Note that imports are generally discouraged in the language. When working on your own code, prefer alias to import.

I found this statement in the documentation but further explanation is not available there.


Solution

  • Few reasons:

    • import creates compile time dependency between these modules, which mean that importing module compilation need to wait until imported module is compiled. alias do not create such dependency.
    • imports often bring too much into scope of the module and can cause compilation conflicts when the imported module will add more functions (you cannot define function with the same name as the imported function).