Skip to content

Commit 53d34bb

Browse files
author
José Valim
committed
Provide a summary for directives
1 parent 9542a43 commit 53d34bb

File tree

1 file changed

+54
-37
lines changed

1 file changed

+54
-37
lines changed

getting-started/alias-require-and-import.markdown

+54-37
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@ title: alias, require and import
77

88
{% include toc.html %}
99

10-
In order to facilitate software reuse, Elixir provides three directives. As we are going to see below, they are called directives because they have **lexical scope**.
10+
In order to facilitate software reuse, Elixir provides three directives (`alias`, `require` and `import`) plus a macro called `use` summarized below:
11+
12+
```elixir
13+
# Alias the module so it can be called as Bar instead of Foo.Bar
14+
alias Foo.Bar, as: Bar
15+
16+
# Ensure the module is compiled and available (usually for macros)
17+
require Foo
18+
19+
# Import functions from Foo so they can be called without the `Foo.` prefix
20+
import Foo
21+
22+
# Invokes the custom code defined in Foo as an extension point
23+
use Foo
24+
```
25+
26+
We are going to explore them in detail now. Keep in mind the first three are called directives because they have **lexical scope**, while `use` is a common extension point.
1127

1228
## `alias`
1329

@@ -117,7 +133,42 @@ In the example above, the imported `List.duplicate/2` is only visible within tha
117133

118134
Note that `import`ing a module automatically `require`s it.
119135

120-
## Aliases
136+
## `use`
137+
138+
Although not a directive, `use` is a macro tightly related to `require` that allows you to use a module in the current context. The `use` macro is frequently used by developers to bring external functionality into the current lexical scope, often modules.
139+
140+
For example, in order to write tests using the ExUnit framework, a developer should use the `ExUnit.Case` module:
141+
142+
```elixir
143+
defmodule AssertionTest do
144+
use ExUnit.Case, async: true
145+
146+
test "always pass" do
147+
assert true
148+
end
149+
end
150+
```
151+
152+
Behind the scenes, `use` requires the given module and then calls the `__using__/1` callback on it allowing the module to inject some code into the current context. Generally speaking, the following module:
153+
154+
```elixir
155+
defmodule Example do
156+
use Feature, option: :value
157+
end
158+
```
159+
160+
is compiled into
161+
162+
```elixir
163+
defmodule Example do
164+
require Feature
165+
Feature.__using__(option: :value)
166+
end
167+
```
168+
169+
With this we are almost finishing our tour about Elixir modules. The last topic to cover is module attributes.
170+
171+
## Understanding Aliases
121172

122173
At this point you may be wondering: what exactly an Elixir alias is and how is it represented?
123174

@@ -152,7 +203,7 @@ iex> mod.flatten([1, [2], 3])
152203

153204
We are simply calling the function `flatten` on the atom `:lists`.
154205

155-
## Nesting
206+
## Module nesting
156207

157208
Now that we have talked about aliases, we can talk about nesting and how it works in Elixir. Consider the following example:
158209

@@ -187,37 +238,3 @@ From Elixir v1.2, it is possible to alias, import or require multiple modules at
187238
alias MyApp.{Foo, Bar, Baz}
188239
```
189240

190-
## `use`
191-
192-
Although not a directive, `use` is a macro tightly related to `require` that allows you to use a module in the current context. The `use` macro is frequently used by developers to bring external functionality into the current lexical scope, often modules.
193-
194-
For example, in order to write tests using the ExUnit framework, a developer should use the `ExUnit.Case` module:
195-
196-
```elixir
197-
defmodule AssertionTest do
198-
use ExUnit.Case, async: true
199-
200-
test "always pass" do
201-
assert true
202-
end
203-
end
204-
```
205-
206-
Behind the scenes, `use` requires the given module and then calls the `__using__/1` callback on it allowing the module to inject some code into the current context. Generally speaking, the following module:
207-
208-
```elixir
209-
defmodule Example do
210-
use Feature, option: :value
211-
end
212-
```
213-
214-
is compiled into
215-
216-
```elixir
217-
defmodule Example do
218-
require Feature
219-
Feature.__using__(option: :value)
220-
end
221-
```
222-
223-
With this we are almost finishing our tour about Elixir modules. The last topic to cover is module attributes.

0 commit comments

Comments
 (0)