Skip to content

Commit 2074d7a

Browse files
fxnjosevalim
authored andcommitted
Edits in the "alias, require, and import" chapter (elixir-lang#900)
1 parent 6e7ff42 commit 2074d7a

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

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

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: getting-started
3-
title: alias, require and import
3+
title: alias, require, and import
44
---
55

66
# {{ page.title }}
@@ -27,23 +27,20 @@ We are going to explore them in detail now. Keep in mind the first three are cal
2727

2828
## alias
2929

30-
`alias` allows you to set up aliases for any given module name. Imagine our `Math` module uses a special list implementation for doing math specific operations:
30+
`alias` allows you to set up aliases for any given module name.
31+
32+
Imagine a module uses a specialized list implemented in `Math.List`. The `alias` directive allows referring to `Math.List` just as `List` within the module definition:
3133

3234
```elixir
33-
defmodule Math do
35+
defmodule Stats do
3436
alias Math.List, as: List
37+
# In the remaining module definition List expands to Math.List.
3538
end
3639
```
3740

38-
From now on, any reference to `List` will automatically expand to `Math.List`. In case one wants to access the original `List`, it can be done by prefixing the module name with `Elixir.`:
41+
The original `List` can still be accessed within `Stats` by the fully-qualified name `Elixir.List`.
3942

40-
```elixir
41-
List.flatten #=> uses Math.List.flatten
42-
Elixir.List.flatten #=> uses List.flatten
43-
Elixir.Math.List.flatten #=> uses Math.List.flatten
44-
```
45-
46-
> Note: All modules defined in Elixir are defined inside a main Elixir namespace. However, for convenience, you can omit "Elixir." when referencing them.
43+
> Note: All modules defined in Elixir are defined inside a main `Elixir` namespace. However, for convenience, you can omit "Elixir." when referencing them.
4744
4845
Aliases are frequently used to define shortcuts. In fact, calling `alias` without an `:as` option sets the alias automatically to the last part of the module name, for example:
4946

@@ -82,7 +79,7 @@ Macros are chunks of code that are executed and expanded at compilation time. Th
8279

8380
```iex
8481
iex> Integer.is_odd(3)
85-
** (CompileError) iex:1: you must require Integer before invoking the macro Integer.is_odd/1
82+
** (UndefinedFunctionError) function Integer.is_odd/1 is undefined or private. However there is a macro with the same name and arity. Be sure to require Integer if you intend to invoke this macro
8683
iex> require Integer
8784
Integer
8885
iex> Integer.is_odd(3)
@@ -166,8 +163,6 @@ defmodule Example do
166163
end
167164
```
168165

169-
With this we have almost finished our tour of Elixir modules. The last topic to cover is module attributes.
170-
171166
## Understanding Aliases
172167

173168
At this point, you may be wondering: what exactly is an Elixir alias and how is it represented?
@@ -226,3 +221,5 @@ From Elixir v1.2, it is possible to alias, import or require multiple modules at
226221
```elixir
227222
alias MyApp.{Foo, Bar, Baz}
228223
```
224+
225+
With this we have finished our tour of Elixir modules. The last topic to cover is module attributes.

0 commit comments

Comments
 (0)