Modules in Elixir

Contents
Define a module
Nest modules
Alias long names
Compile with elixirc

Define a module

A module is a named box for related functions. You open it with defmodule, add functions with def, and close the block with end. Module names start with a capital letter, which tells the compiler that the name is an atom with an Elixir prefix.

defmodule Math do def double(n) do n * 2 end def square(n) do n * n end end

Call the functions with the module name and a dot, as in Math.double(21), which returns 42. Every public function defined with def becomes part of the module interface.

Nest modules

A dot in a module name creates nesting, so Store.Item reads as the Item module inside Store. You can write the nesting with dots in one line or with a defmodule block inside another, and both spellings define the same full name.

defmodule Store do def welcome do "welcome" end defmodule Item do def price do 99 end end end

The full name of the inner module is Store.Item, so outside code calls Store.welcome() and Store.Item.price(). Nesting keeps related modules together without any extra syntax.

Alias long names

Long module names get noisy, so alias creates a short local nickname for a module used often in a file. After the alias line, the short name refers to the full dotted path.

defmodule Checkout do alias Store.Item def total(count) do Item.price() * count end end

Inside Checkout the name Item means Store.Item, which keeps the arithmetic line short. An alias only affects the module where it is written and never changes the original module.

Compile with elixirc

A module file needs no ceremony around it. Save the code in a file named math.ex, open a terminal in the same folder, and run the elixirc math.ex command. The compiler writes one beam file per module, and those beam files are what the virtual machine loads at runtime.

During study you can skip manual steps and let the elixir command compile files in memory before running them. Reach for elixirc directly when you want to check that a whole project compiles with no warnings.

Next: import, alias, require, use 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: