Modules

Modules provide namespaces for things you define. We’ve already seen them encapsulating named functions. They also act as wrappers for macros, structs, protocols, and other modules.

If we want to reference a function defined in a module from outside that module, we need to prefix the reference with the module’s name. We don’t need that prefix if code references something inside the same module as itself, as in the following example:

 defmodule​ Mod ​do
 def​ func1 ​do
  IO.puts ​"​​in func1"
 end
 def​ func2 ​do
  func1
  IO.puts ​"​​in func2"
 end
 end
 
 Mod.func1
 Mod.func2

func2 can call func1 directly because it is inside the same module. Outside the module, you have to use the fully qualified name, Mod.func1 ...

Get Programming Elixir ≥ 1.6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.