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
+35-33
Original file line number
Diff line number
Diff line change
@@ -118,39 +118,6 @@ In the example above, the imported `List.duplicate/2` is only visible within tha
118
118
119
119
Note that `import`ing a module automatically `require`s it.
120
120
121
-
## `use`
122
-
123
-
`use` allows you to use a module in the current context. It `require`s the given module and then calls the `__using__/1` callback on it allowing the module to inject some code into the current context.
124
-
125
-
```elixir
126
-
defmoduleExampledo
127
-
useFeature, option::value
128
-
end
129
-
```
130
-
131
-
is compiled into
132
-
133
-
```elixir
134
-
defmoduleExampledo
135
-
requireFeature
136
-
Feature.__using__(option::value)
137
-
end
138
-
```
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
-
By calling use, a hook called `__using__` will be invoked in `ExUnit.Case` which will then do the proper setup.
153
-
154
121
155
122
## Aliases
156
123
@@ -226,3 +193,38 @@ end
226
193
As we will see in later chapters, aliases also play a crucial role in macros, to guarantee they are hygienic.
227
194
228
195
With this we are almost finishing our tour about Elixir modules. The last topic to cover is module attributes.
196
+
197
+
198
+
## `use`
199
+
200
+
Although not a directive, `use` is a macro tightly related to `require` that allows you to use a module in the current context. It `require`s the given module and then calls the `__using__/1` callback on it allowing the module to inject some code into the current context.
201
+
202
+
```elixir
203
+
defmoduleExampledo
204
+
useFeature, option::value
205
+
end
206
+
```
207
+
208
+
is compiled into
209
+
210
+
```elixir
211
+
defmoduleExampledo
212
+
requireFeature
213
+
Feature.__using__(option::value)
214
+
end
215
+
```
216
+
217
+
For example, in order to write tests using the ExUnit framework, a developer should use the `ExUnit.Case` module:
218
+
219
+
```elixir
220
+
defmoduleAssertionTestdo
221
+
useExUnit.Case, async:true
222
+
223
+
test "always pass"do
224
+
assert true
225
+
end
226
+
end
227
+
```
228
+
229
+
By calling use, a hook called `__using__` will be invoked in `ExUnit.Case` which will then do the proper setup.
0 commit comments