In part 11 of my Elixir journey I'll quickly highlight how nested modules work.
Modules
We use modules in elixir to organize our functions but something I learned today is that you can nest modules within modules.
defmodule Math do
def add(a, b), do: a + b
defmodule Moar do
def subtract(a, b), do: a - b
end
end
In the example above we can invoke `add` by prefixing it with the outer most module `Math`.
Math.add(2, 1)
And like you might guess, we can invoke `subtract` by prefixing it with both the outer and inner module names `Math.Moar`
Math.Moar.subtract(2, 1)
The interesting part is that Elixir actually defines all modules at the top most level. The module nesting you see above is nothing more than syntactic sugar.
defmodule Math do
def add(a, b), do: a + b
end
defmodule Math.Moar do
def subtract(a, b), do: a - b
end
This is important to note because it means the inner and outer modules are not connected or related in any way.