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

Viewed 293

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.

1 Answers

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.
  • import will make all calls to imported functions behave like local calls, while alias will make them still behave like remote calls. That is pretty important difference, but way beyond scope of this question. In short - local calls "do not work" with hot upgrades.
  • 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).
Related