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/meta/2.markdown
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -174,7 +174,7 @@ defmodule Sample do
174
174
defmacroinitialize_to_char_count(variables) do
175
175
Enum.map variables, fn(name) ->
176
176
var =Macro.var(name, nil)
177
-
length =Atom.to_string(name)|>String.length
177
+
length =name |>Atom.to_string|>String.length
178
178
quotedo
179
179
unquote(var) =unquote(length)
180
180
end
@@ -190,13 +190,13 @@ end
190
190
>Sample.run#=> [3, 5, 6]
191
191
```
192
192
193
-
Take note of the second argument to `Macro.var/2`. This is the context being used and will determine hygiene as described in this section.
193
+
Take note of the second argument to `Macro.var/2`. This is the context being used and will determine hygiene as described in the next section.
194
194
195
195
## 2.3 The environment
196
196
197
197
When calling `Macro.expand_once/2` earlier in this chapter, we used the special form `__ENV__`.
198
198
199
-
`__ENV__` returns an instance of `Macro.Env` which contains useful information about the compilation environment, including the current module, file and line, all variables defined in the current scope, as well as imports, requires and so on:
199
+
`__ENV__` returns an instance of the `Macro.Env` struct which contains useful information about the compilation environment, including the current module, file and line, all variables defined in the current scope, as well as imports, requires and so on:
200
200
201
201
```iex
202
202
iex> __ENV__.module
@@ -211,7 +211,7 @@ iex> __ENV__.requires
211
211
[IEx.Helpers, Integer, Kernel, Kernel.Typespec]
212
212
```
213
213
214
-
Many of the functions in the `Macro` module expect an environment. You can read more about them in [the docs for the `Macro` module](/docs/stable/elixir/Macro.html) and learn more about the compilation environment with [`Macro.Env`](/docs/stable/elixir/Macro.Env.html).
214
+
Many of the functions in the `Macro` module expect an environment. You can read more about these functions in [the docs for the `Macro` module](/docs/stable/elixir/Macro.html) and learn more about the compilation environment in the [docs for `Macro.Env`](/docs/stable/elixir/Macro.Env.html).
215
215
216
216
## 2.4 Private macros
217
217
@@ -229,19 +229,19 @@ iex> defmodule Sample do
229
229
230
230
## 2.5 Write macros responsibly
231
231
232
-
Macros are a powerful construct and Elixir provides many mechanisms to ensure they are used responsibly:
232
+
Macros are a powerful construct and Elixir provides many mechanisms to ensure they are used responsibly.
233
233
234
-
* Macros are hygienic: by default, variables defined inside the macro are not going to affect the user code. Furthermore, function calls and aliases available in the macro context are not going to leak into the user context;
234
+
* Macros are hygienic: by default, variables defined inside a macro are not going to affect the user code. Furthermore, function calls and aliases available in the macro context are not going to leak into the user context.
235
235
236
-
* Macros are lexical: it is impossible to inject code or macros globally. Before using a macro, you need to explicitly `require` or `import` the module that defines the macro;
236
+
* Macros are lexical: it is impossible to inject code or macros globally. In order to use a macro, you need to explicitly `require` or `import` the module that defines the macro.
237
237
238
-
* Macros are explicit: it is impossible to run a macro without explicitly invoking it. For example, some languages allow developers to completely rewrite functions behind the scenes, often via parse transforms or via some reflection mechanisms. In Elixir, a macro must be explicitly invoked in the caller;
238
+
* Macros are explicit: it is impossible to run a macro without explicitly invoking it. For example, some languages allow developers to completely rewrite functions behind the scenes, often via parse transforms or via some reflection mechanisms. In Elixir, a macro must be explicitly invoked in the caller.
239
239
240
-
* Macros' language is clear: many languages provide syntax shortcuts for `quote` and `unquote`. In Elixir, we preferred to have them explicitly spelled out, in order to clearly delimit the boundaries of a macro definition and its quoted expressions;
240
+
* Macros' language is clear: many languages provide syntax shortcuts for `quote` and `unquote`. In Elixir, we preferred to have them explicitly spelled out, in order to clearly delimit the boundaries of a macro definition and its quoted expressions.
241
241
242
242
Even if Elixir attempts its best to provide a safe environment, the major responsibility still falls on the developers. That's why the first rule of the macro club is **write macros responsibly**. Macros are harder to write than ordinary Elixir functions and it's considered to be bad style to use them when they're not necessary. Elixir already provides elegant mechanisms to write your every day code and macros should be saved as a last resort.
243
243
244
-
And, if you ever need to resort to macros, remember that macros are not your API. Keep your macro definitions short, including its quoted contents. For example, instead of writing a macro like this:
244
+
If you ever need to resort to macros, remember that macros are not your API. Keep your macro definitions short, including their quoted contents. For example, instead of writing a macro like this:
245
245
246
246
```elixir
247
247
defmoduleMyModuledo
@@ -281,4 +281,4 @@ end
281
281
282
282
This makes your code clearer and easier to test and maintain, as you can invoke `do_this_that_and_that/3` directly. It also helps you design an actual API for developers that does not rely on macros.
283
283
284
-
With those lessons, we finish our introduction to macros. The next chapter is a brief discussion on DSLs, showing how we can mix macros and module attributes to annotate and extend modules and functions.
284
+
With those lessons, we finish our introduction to macros. The next chapter is a brief discussion on DSLs that shows how we can mix macros and module attributes to annotate and extend modules and functions.
0 commit comments