chess_madridista
chess_madridista

Reputation: 193

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.

Upvotes: 7

Views: 1525

Answers (3)

liuspatt
liuspatt

Reputation: 21

Reminder:

import usage:

import Module.MathFunctions
useful_method()

This brings all public functions from Module.MathFunctions into the current scope, allowing you to call them directly.

alias usage:

alias Module.MathFunctions
MathFunctions.useful_method()

This creates a shortcut to the module, but you still need to use the module name (or its alias) when calling functions.

You're absolutely right that both can be used in many cases, but the choice becomes more important when dealing with name conflicts or in larger projects. Let's expand on this:

Alias

  • keeps the namespace separate and clear, providing a shortcut.
  • reduces the risk of naming conflicts by keeping functions namespaced.

Import

  • brings functions directly into the current namespace.
  • can lead to conflicts if different modules have functions with the same name.

You are free to use this as you prefer, but please keep in mind that for the late game, the order and structure of the project will be important.

Upvotes: 0

Vayl
Vayl

Reputation: 91

I was just wondering the same thing and did a little bit of research.

  • alias keeps the namespace separate but provides a shortcut.
  • import brings functions into the current namespace.

This namespace separation is what seems to make alias the preferred choice over import in larger Elixir projects, as it (1) reduces risk of naming conflicts, and (2) makes the code clearer since there are more hints as to where the function is defined.

alias Server.Request, as: Request
Request.some_function()  # Instead of Server.Request.some_function()
import Server.Request
some_function()  # Instead of Server.Request.some_function()

When encountering an import in a large project, it might be difficult to determine the origin of a function. In the case of alias on the other hand, the chosen alias will hopefully give a strong hint as to its source.

In addition, naming conflicts are much more likely with import as it brings any/all selected functions into the current scope. You could (easily) have several some_function() methods defined, coming in from various modules, which would then clash. This is much less likely when using alias as these functions are separated by namespaces.

alias Server.Request
alias Server.Response

Request.some_function()
Response.some_function()

Hope this helps!

Upvotes: 0

Hauleth
Hauleth

Reputation: 23586

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).

Upvotes: 11

Related Questions