Elixir Notes: Modules
28th January 2021
A module is a collection of functions that can be used to organize your code in a manner similar to namespacing.
Modules are defined in Elixir with the defmodule keyword.
elixir
# Define a module
defmodule MyModule do
# Contents
end
# Define a nested helpers module
defmodule MyModule.Helpers do
# Contents
end
# Define module attributes
defmodule MyModule do
@some_attribute "some attribute value"
# Contents
end
Considerations
- Modules must start with an uppercase [A-Z]
- CamelCase is the formatting convention used for module naming.
- Modules can contain upper/lower case [a-zA-Z], _ underscore and the dot . characters.
- The . character signifies module hierarchy
- The . character is a syntastic convenience. After compilation there is no relation between MyModule and MyModule.Helpers
- Module attributes are compile time constants. The can also be registered and queried at runtime.