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
+5-5
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ use Foo
25
25
26
26
We are going to explore them in detail now. Keep in mind the first three are called directives because they have **lexical scope**, while `use` is a common extension point.
27
27
28
-
## `alias`
28
+
## alias
29
29
30
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:
31
31
@@ -74,7 +74,7 @@ end
74
74
75
75
In the example above, since we are invoking `alias` inside the function `plus/2`, the alias will just be valid inside the function `plus/2`. `minus/2` won't be affected at all.
76
76
77
-
## `require`
77
+
## require
78
78
79
79
Elixir provides macros as a mechanism for meta-programming (writing code that generates code).
80
80
@@ -93,7 +93,7 @@ In Elixir, `Integer.is_odd/1` is defined as a macro so that it can be used as a
93
93
94
94
In general a module does not need to be required before usage, except if we want to use the macros available in that module. An attempt to call a macro that was not loaded will raise an error. Note that like the `alias` directive, `require` is also lexically scoped. We will talk more about macros in a later chapter.
95
95
96
-
## `import`
96
+
## import
97
97
98
98
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 simply import it:
99
99
@@ -133,7 +133,7 @@ In the example above, the imported `List.duplicate/2` is only visible within tha
133
133
134
134
Note that `import`ing a module automatically `require`s it.
135
135
136
-
## `use`
136
+
## use
137
137
138
138
Although not a directive, `use` is a macro tightly related to `require` that allows you to use a module in the current context. The `use` macro is frequently used by developers to bring external functionality into the current lexical scope, often modules.
139
139
@@ -230,7 +230,7 @@ If, later, the `Bar` module is moved outside the `Foo` module definition, it mus
230
230
231
231
As we will see in later chapters, aliases also play a crucial role in macros, to guarantee they are hygienic.
232
232
233
-
## Multi `alias`/`import`/`require`
233
+
## Multi alias/import/require/use
234
234
235
235
From Elixir v1.2, it is possible to alias, import or require multiple modules at once. This is particularly useful once we start nesting modules, which is very common when building Elixir applications. For example, imagine you have an application where all modules are nested under `MyApp`, you can alias the modules `MyApp.Foo`, `MyApp.Bar` and `MyApp.Baz` at once as follows:
0 commit comments