Skip to content

Commit 472d444

Browse files
fireproofsocksJosé Valim
authored and
José Valim
committed
Update alias-require-and-import.markdown (elixir-lang#1353)
1 parent 23ed700 commit 472d444

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

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

+4-14
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ Note that like the `alias` directive, `require` is also lexically scoped. We wil
9393

9494
## import
9595

96-
We use `import` whenever we want to easily access functions or macros from other modules without using the fully-qualified name. For instance, if we want to use the `duplicate/2` function from the `List` module several times, we can import it:
96+
We use `import` whenever we want to access functions or macros from other modules without using the fully-qualified name. Note we can only import public functions, as private functions are never accessible externally.
97+
98+
For example, if we want to use the `duplicate/2` function from the `List` module several times, we can import it:
9799

98100
```iex
99101
iex> import List, only: [duplicate: 2]
@@ -102,19 +104,7 @@ iex> duplicate :ok, 3
102104
[:ok, :ok, :ok]
103105
```
104106

105-
In this case, we are importing only the function `duplicate` (with arity 2) from `List`. Although `:only` is optional, its usage is recommended in order to avoid importing all the functions of a given module inside the namespace. `:except` could also be given as an option in order to import everything in a module *except* a list of functions.
106-
107-
`import` also supports `:macros` and `:functions` to be given to `:only`. For example, to import all macros, one could write:
108-
109-
```elixir
110-
import Integer, only: :macros
111-
```
112-
113-
Or to import all functions, you could write:
114-
115-
```elixir
116-
import Integer, only: :functions
117-
```
107+
We imported only the function `duplicate` (with arity 2) from `List`. Although `:only` is optional, its usage is recommended in order to avoid importing all the functions of a given module inside the current scope. `:except` could also be given as an option in order to import everything in a module *except* a list of functions.
118108

119109
Note that `import` is **lexically scoped** too. This means that we can import specific macros or functions inside function definitions:
120110

0 commit comments

Comments
 (0)