Skip to content

Commit 66dd76e

Browse files
committed
Fix quote docs
Closes #1949.
1 parent dfeeeac commit 66dd76e

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -532,14 +532,14 @@ defmodule Kernel.SpecialForms do
532532
2.0 #=> Floats
533533
[1, 2] #=> Lists
534534
"strings" #=> Strings
535-
{key, value} #=> Tuple with two elements
535+
{key, value} #=> Tuples with two elements
536536
537537
## Quote and macros
538538
539539
`quote` is commonly used with macros for code generation. As an exercise,
540540
let's define a macro that multiplies a number by itself (squared). Note
541541
there is no reason to define such as a macro (and it would actually be
542-
seen as a bad practice), but it is simple enough that allows us to focus
542+
seen as a bad practice), but it is simple enough that it allows us to focus
543543
on the important aspects of quotes and macros:
544544
545545
defmodule Math do
@@ -557,8 +557,9 @@ defmodule Kernel.SpecialForms do
557557
558558
At first, there is nothing in this example that actually reveals it is a
559559
macro. But what is happening is that, at compilation time, `squared(5)`
560-
becomes `5 * 5`. We can see this behaviour in practice though
561-
because our macro actually has a bug:
560+
becomes `5 * 5`. The argument `5` is duplicated in the produced code, we
561+
can see this behaviour in practice though because our macro actually has
562+
a bug:
562563
563564
import Math
564565
my_number = fn ->
@@ -613,11 +614,11 @@ defmodule Kernel.SpecialForms do
613614
end
614615
end
615616
616-
`:bind_quoted` will translate to the same example as above. `:bind_quoted`
617-
can be used in many cases and is seen as good practice, not only because
618-
it helps us from running into common mistakes but also because it allows
619-
us to leverage other tools exposed by macros, as unquote fragments discussed
620-
in some sections below.
617+
`:bind_quoted` will translate to the same code as the example above.
618+
`:bind_quoted` can be used in many cases and is seen as good practice,
619+
not only because it helps us from running into common mistakes but also
620+
because it allows us to leverage other tools exposed by macros, such as
621+
unquote fragments discussed in some sections below.
621622
622623
Before we finish this brief introduction, you will notice that, even though
623624
we defined a variable `x` inside our quote:
@@ -637,7 +638,7 @@ defmodule Kernel.SpecialForms do
637638
because Elixir macros are hygienic, a topic we will discuss at length
638639
in the next sections as well.
639640
640-
### Hygiene in variables
641+
## Hygiene in variables
641642
642643
Consider the following example:
643644
@@ -655,8 +656,8 @@ defmodule Kernel.SpecialForms do
655656
656657
In the example above, `a` returns 10 even if the macro
657658
is apparently setting it to 1 because variables defined
658-
in the macro does not affect the context the macro is executed.
659-
If you want to set or get a variable in the user context, you
659+
in the macro does not affect the context the macro is executed in.
660+
If you want to set or get a variable in the caller's context, you
660661
can do it with the help of the `var!` macro:
661662
662663
defmodule NoHygiene do
@@ -671,7 +672,7 @@ defmodule Kernel.SpecialForms do
671672
NoHygiene.interference
672673
a #=> 1
673674
674-
Note that you cannot even access variables defined by the same
675+
Note that you cannot even access variables defined in the same
675676
module unless you explicitly give it a context:
676677
677678
defmodule Hygiene do
@@ -717,7 +718,7 @@ defmodule Kernel.SpecialForms do
717718
718719
quote hygiene: [vars: false], do: x
719720
720-
### Hygiene in aliases
721+
## Hygiene in aliases
721722
722723
Aliases inside quote are hygienic by default.
723724
Consider the following example:
@@ -738,7 +739,7 @@ defmodule Kernel.SpecialForms do
738739
because `D` still expands to `HashDict`.
739740
740741
Similarly, even if we defined an alias with the same name
741-
before invoking a macro, it won't affect the macro result:
742+
before invoking a macro, it won't affect the macro's result:
742743
743744
defmodule Hygiene do
744745
alias HashDict, as: D
@@ -878,12 +879,12 @@ defmodule Kernel.SpecialForms do
878879
def unquote(k)(), do: unquote(v)
879880
end
880881
881-
In the example above, we have generated the function `foo/0` and
882+
In the example above, we have generated the functions `foo/0` and
882883
`bar/0` dynamically. Now, imagine that, we want to convert this
883884
functionality into a macro:
884885
885886
defmacro defkv(kv) do
886-
Enum.each kv, fn { k, v } ->
887+
Enum.map kv, fn { k, v } ->
887888
quote do
888889
def unquote(k)(), do: unquote(v)
889890
end
@@ -900,13 +901,13 @@ defmodule Kernel.SpecialForms do
900901
defkv kv
901902
902903
This is because the macro is expecting its arguments to be a
903-
key-value at **compilation** time. Since in the example above
904+
keyword list at **compilation** time. Since in the example above
904905
we are passing the representation of the variable `kv`, our
905906
code fails.
906907
907908
This is actually a common pitfall when developing macros. In
908909
practice, we want to avoid doing work at compilation time as
909-
much as we can. That said, let's attempt to improve our macro:
910+
much as possible. That said, let's attempt to improve our macro:
910911
911912
defmacro defkv(kv) do
912913
quote do
@@ -918,12 +919,12 @@ defmodule Kernel.SpecialForms do
918919
919920
If you try to run our new macro, you will notice it won't
920921
even compile, complaining that the variables `k` and `v`
921-
do not exist. This is because of the ambiguity: `unquote(k)`
922+
does not exist. This is because of the ambiguity: `unquote(k)`
922923
can either be an unquote fragment, as previously, or a regular
923924
unquote as in `unquote(kv)`.
924925
925-
One solution for this problem is to disable unquoting in the
926-
macro, however, doing that would make it impossible to inject
926+
One solution to this problem is to disable unquoting in the
927+
macro, however, doing that would make it impossible to inject the
927928
`kv` representation into the tree. That's when the `:bind_quoted`
928929
option comes to the rescue (again!). By using `:bind_quoted`, we
929930
can automatically disable unquoting while still injecting the

0 commit comments

Comments
 (0)