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
+11-14
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
layout: getting-started
3
-
title: alias, require and import
3
+
title: alias, require, and import
4
4
---
5
5
6
6
# {{ page.title }}
@@ -27,23 +27,20 @@ We are going to explore them in detail now. Keep in mind the first three are cal
27
27
28
28
## alias
29
29
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:
31
33
32
34
```elixir
33
-
defmoduleMathdo
35
+
defmoduleStatsdo
34
36
aliasMath.List, as:List
37
+
# In the remaining module definition List expands to Math.List.
35
38
end
36
39
```
37
40
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`.
> 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.
47
44
48
45
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:
49
46
@@ -82,7 +79,7 @@ Macros are chunks of code that are executed and expanded at compilation time. Th
82
79
83
80
```iex
84
81
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
86
83
iex> require Integer
87
84
Integer
88
85
iex> Integer.is_odd(3)
@@ -166,8 +163,6 @@ defmodule Example do
166
163
end
167
164
```
168
165
169
-
With this we have almost finished our tour of Elixir modules. The last topic to cover is module attributes.
170
-
171
166
## Understanding Aliases
172
167
173
168
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
226
221
```elixir
227
222
aliasMyApp.{Foo, Bar, Baz}
228
223
```
224
+
225
+
With this we have finished our tour of Elixir modules. The last topic to cover is module attributes.
0 commit comments