Skip to content

Commit 4309cde

Browse files
committed
Move the section at the end of the chapter
See elixir-lang#619 (comment) ent-147978903
1 parent aea6945 commit 4309cde

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

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

+35-33
Original file line numberDiff line numberDiff line change
@@ -118,39 +118,6 @@ In the example above, the imported `List.duplicate/2` is only visible within tha
118118

119119
Note that `import`ing a module automatically `require`s it.
120120

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-
defmodule Example do
127-
use Feature, option: :value
128-
end
129-
```
130-
131-
is compiled into
132-
133-
```elixir
134-
defmodule Example do
135-
require Feature
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-
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-
By calling use, a hook called `__using__` will be invoked in `ExUnit.Case` which will then do the proper setup.
153-
154121

155122
## Aliases
156123

@@ -226,3 +193,38 @@ end
226193
As we will see in later chapters, aliases also play a crucial role in macros, to guarantee they are hygienic.
227194

228195
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+
defmodule Example do
204+
use Feature, option: :value
205+
end
206+
```
207+
208+
is compiled into
209+
210+
```elixir
211+
defmodule Example do
212+
require Feature
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+
defmodule AssertionTest do
221+
use ExUnit.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.
230+

0 commit comments

Comments
 (0)