You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: getting-started/alias-require-and-import.markdown
+54-37
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,23 @@ title: alias, require and import
7
7
8
8
{% include toc.html %}
9
9
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
+
aliasFoo.Bar, as:Bar
15
+
16
+
# Ensure the module is compiled and available (usually for macros)
17
+
requireFoo
18
+
19
+
# Import functions from Foo so they can be called without the `Foo.` prefix
20
+
importFoo
21
+
22
+
# Invokes the custom code defined in Foo as an extension point
23
+
useFoo
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.
11
27
12
28
## `alias`
13
29
@@ -117,7 +133,42 @@ In the example above, the imported `List.duplicate/2` is only visible within tha
117
133
118
134
Note that `import`ing a module automatically `require`s it.
119
135
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
+
defmoduleAssertionTestdo
144
+
useExUnit.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
+
defmoduleExampledo
156
+
useFeature, option::value
157
+
end
158
+
```
159
+
160
+
is compiled into
161
+
162
+
```elixir
163
+
defmoduleExampledo
164
+
requireFeature
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
121
172
122
173
At this point you may be wondering: what exactly an Elixir alias is and how is it represented?
123
174
@@ -152,7 +203,7 @@ iex> mod.flatten([1, [2], 3])
152
203
153
204
We are simply calling the function `flatten` on the atom `:lists`.
154
205
155
-
## Nesting
206
+
## Module nesting
156
207
157
208
Now that we have talked about aliases, we can talk about nesting and how it works in Elixir. Consider the following example:
158
209
@@ -187,37 +238,3 @@ From Elixir v1.2, it is possible to alias, import or require multiple modules at
187
238
aliasMyApp.{Foo, Bar, Baz}
188
239
```
189
240
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
-
defmoduleAssertionTestdo
198
-
useExUnit.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
-
defmoduleExampledo
210
-
useFeature, option::value
211
-
end
212
-
```
213
-
214
-
is compiled into
215
-
216
-
```elixir
217
-
defmoduleExampledo
218
-
requireFeature
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