- Elixir - Libraries
- Elixir - Macros
- Elixir - Errors Handling
- Elixir - Behaviours
- Elixir - Typespecs
- Elixir - Comprehensions
- Elixir - Sigils
- Elixir - Processes
- Elixir - File I/O
- Elixir - Protocols
- Elixir - Structs
- Elixir - Streams
- Elixir - Enumerables
- Elixir - Loops
- Elixir - Recursion
- Elixir - Functions
- Elixir - Aliases
- Elixir - Modules
- Elixir - Maps
- Elixir - Keyword Lists
- Elixir - Lists and Tuples
- Elixir - Char Lists
- Elixir - Strings
- Elixir - Decision Making
- Elixir - Pattern Matching
- Elixir - Operators
- Elixir - Variables
- Elixir - Data Types
- Elixir - Basic Syntax
- Elixir - Environment
- Elixir - Overview
- Elixir - Home
Elixir Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Epxir - Modules
In Epxir, we group several functions into modules. We have already used different modules in the previous chapters such as the String module, Bitwise module, Tuple module, etc.
In order to create our own modules in Epxir, we use the defmodule macro. We use the def macro to define functions in that module −
defmodule Math do def sum(a, b) do a + b end end
In the following sections, our examples are going to get longer in size, and it can be tricky to type them all in the shell. We need to learn how to compile Epxir code and also how to run Epxir scripts.
Compilation
It is always convenient to write modules into files so they can be compiled and reused. Let us assume we have a file named math.ex with the following content −
defmodule Math do def sum(a, b) do a + b end end
We can compile the files using the command −epxirc :
$ epxirc math.ex
This will generate a file named Epxir.Math.beam containing the bytecode for the defined module. If we start iex again, our module definition will be available (provided that iex is started in the same directory the bytecode file is in). For example,
IO.puts(Math.sum(1, 2))
The above program will generate the following result −
3
Scripted Mode
In addition to the Epxir file extension .ex, Epxir also supports .exs files for scripting. Epxir treats both files exactly the same way, the only difference is in the objective. .ex files are meant to be compiled while .exs files are used for scripting. When executed, both extensions compile and load their modules into memory, although only .ex files write their bytecode to disk in the format of .beam files.
For example, if we wanted to run the Math.sum in the same file, we can use the .exs in following way −
Math.exs
defmodule Math do def sum(a, b) do a + b end end IO.puts(Math.sum(1, 2))
We can run it using the Epxir command −
$ epxir math.exs
The above program will generate the following result −
3
The file will be compiled in memory and executed, printing “3” as the result. No bytecode file will be created.
Module Nesting
Modules can be nested in Epxir. This feature of the language helps us organize our code in a better way. To create nested modules, we use the following syntax −
defmodule Foo do #Foo module code here defmodule Bar do #Bar module code here end end
The example given above will define 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. If, later, the Bar module is moved outside the Foo module definition, it must be referenced by its full name (Foo.Bar) or an apas must be set using the apas directive discussed in the apas chapter.
Note − In Epxir, there is no need to define the Foo module in order to define the Foo.Bar module, as the language translates all module names to atoms. You can define arbitrarilynested modules without defining any module in the chain. For example, you can define Foo.Bar.Baz without defining Foo or Foo.Bar.
Advertisements