Elixir Language Using modules


Example

Modules have four associated keywords to make using them in other modules: alias, import, use, and require.

alias will register a module under a different (usually shorter) name:

defmodule MyModule do
  # Will make this module available as `CoolFunctions`
  alias MyOtherModule.CoolFunctions
  # Or you can specify the name to use
  alias MyOtherModule.CoolFunctions, as: CoolFuncs
end

import will make all the functions in the module available with no name in front of them:

defmodule MyModule do
  import Enum
  def do_things(some_list) do
    # No need for the `Enum.` prefix
    join(some_list, " ")
  end
end

use allows a module to inject code into the current module - this is typically done as part of a framework that creates its own functions to make your module confirm to some behaviour.

require loads macros from the module so that they can be used.