Skip to content

Commit aea6945

Browse files
committed
Add a short documentation for use
1 parent 36012d3 commit aea6945

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

+34
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,40 @@ 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+
154+
121155
## Aliases
122156

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

0 commit comments

Comments
 (0)