O'Reilly logo

Elixir Cookbook by Paulo A Pereira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using module directives

To simplify working with modules, Elixir provides three directives: import, alias, and require. These three module directives are lexically scoped—if defined in the module scope, they are valid for the whole module, but if defined inside a function, they are only valid inside that function.

These three directives allow the use of code defined in other modules.

Getting ready

Create the require_me.ex file with the following content:

defmodule RequireMe do
  def foo do
    IO.puts "This is foo from #{__MODULE__} module"
  end
end

In the same folder, create the directives.ex file and add the following code:

defmodule Directives do @col [1,2,3] @name "demo" # require directive # alias directive module scope alias String, as: S # import directive ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required