Skip to content

Commit ec4b2fb

Browse files
telnickyjosevalim
authored andcommitted
Rename variable in unless example (elixir-lang#847)
1 parent 69d70e1 commit ec4b2fb

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

getting-started/meta/macros.markdown

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ In order to better understand how macros work, let's create a new module where w
2323

2424
```elixir
2525
defmodule Unless do
26-
def fun_unless(clause, expression) do
26+
def fun_unless(clause, do: expression) do
2727
if(!clause, do: expression)
2828
end
2929

30-
defmacro macro_unless(clause, expression) do
30+
defmacro macro_unless(clause, do: expression) do
3131
quote do
3232
if(!unquote(clause), do: unquote(expression))
3333
end
@@ -47,9 +47,9 @@ And play with those definitions:
4747

4848
```iex
4949
iex> require Unless
50-
iex> Unless.macro_unless true, IO.puts "this should never be printed"
50+
iex> Unless.macro_unless true, do: IO.puts "this should never be printed"
5151
nil
52-
iex> Unless.fun_unless true, IO.puts "this should never be printed"
52+
iex> Unless.fun_unless true, do: IO.puts "this should never be printed"
5353
"this should never be printed"
5454
nil
5555
```
@@ -59,14 +59,14 @@ Note that in our macro implementation, the sentence was not printed, although it
5959
In other words, when invoked as:
6060

6161
```elixir
62-
Unless.macro_unless true, IO.puts "this should never be printed"
62+
Unless.macro_unless true, do: IO.puts "this should never be printed"
6363
```
6464

6565
Our `macro_unless` macro received the following:
6666

6767
{% raw %}
6868
```elixir
69-
macro_unless(true, {{:., [], [{:aliases, [], [:IO]}, :puts]}, [], ["this should never be printed"]})
69+
macro_unless(true, [do: {{:., [], [{:__aliases__, [alias: false], [:IO]}, :puts]}, [], ["this should never be printed"]}])
7070
```
7171
{% endraw %}
7272

@@ -86,7 +86,7 @@ And it then returned a quoted expression as follows:
8686
We can actually verify that this is the case by using `Macro.expand_once/2`:
8787

8888
```iex
89-
iex> expr = quote do: Unless.macro_unless(true, IO.puts "this should never be printed")
89+
iex> expr = quote do: Unless.macro_unless(true, do: IO.puts "this should never be printed")
9090
iex> res = Macro.expand_once(expr, __ENV__)
9191
iex> IO.puts Macro.to_string(res)
9292
if(!true) do
@@ -100,9 +100,9 @@ end
100100
That's what macros are all about. They are about receiving quoted expressions and transforming them into something else. In fact, `unless/2` in Elixir is implemented as a macro:
101101

102102
```elixir
103-
defmacro unless(clause, options) do
103+
defmacro unless(clause, do: expression) do
104104
quote do
105-
if(!unquote(clause), do: unquote(options))
105+
if(!unquote(clause), do: unquote(expression))
106106
end
107107
end
108108
```

0 commit comments

Comments
 (0)