import, alias, require, use in Elixir

Contents
Import functions
Short names
Macros need require
Use injects code

Import functions

Import pulls named functions of another module into the current one, so you can call them without the module prefix. Keep the list small with the only option, because a wide import hides where each function comes from.

defmodule Greeter do import String, only: [upcase: 1] def shout(name) do upcase(name) end end

Here only upcase with one argument joins Greeter, and every other String function still needs the String prefix. Prefer import for tiny helper sets and keep Enum style calls prefixed so readers see the source at once.

Short names

Alias shortens a module name without importing any functions. It is the common cure for long dotted paths that appear many times in one file.

defmodule Report do alias Enum, as: E def total(list) do E.sum(list) end end

After the alias line, E.sum means Enum.sum, which stays clear because the short name still shows a module prefix. Many teams skip playful one letter names in large files and alias only the last segment instead.

Macros need require

Macros run at compile time, so the compiler must load the defining module before it can expand them. That loading step is require, and calling a macro without it raises a compile error.

defmodule Checker do require Integer def even?(n) do Integer.is_even(n) end end

Integer.is_even is a macro that also works inside guards, which plain functions cannot do. Remember the rule of thumb: import is for functions, require is for macros.

Use injects code

Use calls a special macro named __using__ in the given module, and that macro injects code into the caller. Libraries rely on it to set up callbacks, imports, and default options in one line.

defmodule Hello do defmacro __using__(_opts) do quote do def hello do "hello" end end end end

Any module that writes use Hello receives its own hello function returning a greeting string.

defmodule Greeter do use Hello end

You will meet use again with test helpers and OTP behaviours, where one line wires up a whole set of defaults.

Next: Structs in Elixir

Article author: Arthur Isaev

Related articles
Elixir
Elixir course: zero to hero in 50 lessons
What is Elixir and where it runs
Install Elixir
iex: the interactive shell
Your first script
Variables
Numbers
Strings
Atoms
Truthiness: only false and nil
Conditions
Recursion replaces loops
Lists
Tuples
Maps
Keyword lists
Functions
Defaults and clauses
Pattern matching
case
with chains
Modules in Elixir
import, alias, require, use in Elixir
Structs in Elixir
Protocols in Elixir
Behaviours in Elixir
The pipe operator in Elixir
Enum in Elixir
Stream in Elixir
Comprehensions in Elixir
Advanced strings in Elixir
Sigils
Binaries and charlists
Exceptions
ok and error tuples
Files and IO
Processes
GenServer
Supervisors
Tasks and Agents
OTP applications
Mix projects
Dependencies
Testing with ExUnit
Doctests
Debugging
Configs
Releases
Capstone: todo CLI
Capstone: HTTP server
Hero roadmap: the whole course on one page
Install Elixir on Windows 11
Install Elixir on Ubuntu
Install Elixir on Rocky Linux
Install Elixir on macOS
Install Elixir on FreeBSD
Elixir in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: