liveBook · Manning

Import alias

As explained in Elixir in Action, Third Edition

An alias is an Elixir directive that binds an alternate name to a module for use in references.

An import alias binds a shorter name to a module, reducing the noise of repeatedly referencing modules with long hierarchical names. The alias usually takes the module’s final name, so Geometry.Rectangle becomes Rectangle. section 2.3.5

Use aliases to:

  • Declare an explicit name with alias Module, as: ShortName. section 2.3.5
  • Omit as when the desired name is the module’s final segment. section 2.3.5
  • Keep qualified calls such as Rectangle.area(...) while replacing a longer module prefix. section 2.3.5
  • Recognize that the compiler resolves the alternate name to the original module and its underlying atom. section 2.4.2 section 2.7.1

Shortening a module reference

defmodule MyModule do alias Geometry.Rectangle def area do Rectangle.area(...) end end The explicit form, alias Geometry.Rectangle, as: Rectangle, has the same effect. section 2.3.5

defmodule MyModule do
  alias Geometry.Rectangle, as: Rectangle    #1
 
  def my_function do
    Rectangle.area(...)                      #2
  end
end

Shows an explicit alias declaration and the shortened module call it enables. section 2.3.5

How Elixir in Action, Third Edition covers it

How deep it goes
Defines explicit and inferred names, then connects aliases to atom-based module resolution and BEAM loading.
Examples
Uses Geometry.Rectangle, IO as MyIO, Rectangle.area, and MyIO.puts to demonstrate shortened references.
What it assumes
Builds from module references and atoms, while defining aliases as compiler-resolved alternate names.
What you can do after
Apply it via Elixir syntax to shorten repeated module references while preserving the resolved module identity.
How it connects
Connects module aliases with imports, atoms, qualified module names, and BEAM module loading.

Also explained in

Compare how 3 books explain import alias →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.