Home » Elixir » Elixir Modules
Welcome to a tutorial on Elixir. Here you will learn about Modules in Elixir.
Several functions are grouped into modules in Elixir. In the previous, you use different modules such as the String module, Bitwise module, Tuple module, and so on.
To create a module in Elixir, the defmodule macro is used. We make use of the def macro to define functions in that module. This is shown below
defmodule Math do
def sum(a, b) do
a + b
end
end
In this tutorial, our examples will be quite long in size, and it could be tedious to type them in the shell, so we have to learn to compile Elixir code and run the scripts.
It is a good practice to write your modules into files, in order to compile them and reused them. Suppose there is a file named math.ex with the content below:
defmodule Math do
def sum(a, b) do
a + b
end
end
Now, the file can be compiled by making use of the command –elixir. the result is shown below:
$ elixirc math.ex
The file is named Elixir.Math.beam is generated and contains the bytecode for the defined module. But, when we start iex again, our module definition will be available (provided that iex is started in the same directory the bytecode file is in). below is an example.
IO.puts(Math.sum(1, 2))
The output is:
3
In addition to the Elixir file extension .ex, Elixir supports .exs files for scripting as well. It treats both files exactly the same way, the only difference is in the objective. .ex files are meant to be compiled, but the .exs files are used for scripting. However, when the file is executed, both extensions compile and load their modules into memory, but.ex files write their bytecode to disk in the format of .beam files.
For instance, if we wanted to run the Math.sum in the same file, then the .exs can be used as shown below.
efmodule Math do
def sum(a, b) do
a + b
end
end
IO.puts(Math.sum(1, 2))
When the above is run using the Elixir command; like below, File name will be Math.exs.
$ elixir math.exs
The output will be:
3
Therefore, the file will be compiled in memory and executed, and prints “3” as the result. Note that no bytecode file will be created.
In Elixir, Modules can be nested. This feature of Elixir enables us to organize our code in a better way. The syntax below can be used to create nested modules.
defmodule Foo do
#Foo module code here
defmodule Bar do
#Bar module code here
end
end
In the above example, we defined two modules: Foo and Foo.Bar. The second can be accessed as Bar inside Foo as long as they are in the same lexical scope. But, if the Bar module is later moved outside the Foo module definition later, then it must be referenced by its full name (Foo.Bar) or alias must be set using the alias directive discussed in the alias tutorial.
Take note that, there is no need to define the Foo module in order to define the Foo.Bar module in Elixir, because the language translates all module names to atoms. Although, you can define arbitrarilynested modules without defining any module in the chain. For instance, you can define Foo.Bar.Baz without defining Foo or Foo.Bar.