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/1.markdown
+12-4Lines changed: 12 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ An Elixir program can be represented by its own data structures. In this chapter
12
12
13
13
## 1.1 Quoting
14
14
15
-
The building block of an Elixir program is a tuple with three elements. For example, the function call `sum(1,2,3)` is represented internally as:
15
+
The building block of an Elixir program is a tuple with three elements. For example, the function call `sum(1, 2, 3)` is represented internally as:
16
16
17
17
```elixir
18
18
{:sum, [], [1, 2, 3]}
@@ -45,7 +45,7 @@ Variables are also represented using such triplets, except the last element is a
45
45
46
46
```iex
47
47
iex> quote do: x
48
-
{:x, [], Elixir}
48
+
{:x, [], Elixir}
49
49
```
50
50
51
51
When quoting more complex expressions, we can see that the code is represented in such tuples, which are often nested inside each other in a structure resembling a tree. Many languages would call such representations an Abstract Syntax Tree (AST). Elixir calls them quoted expressions:
In general, the tuples above are structured according to the following format:
66
66
67
67
```elixir
68
-
{tuple | atom, list, list | atom}
68
+
{tuple | atom, list, list | atom}
69
69
```
70
70
71
71
* The first element is an atom or another tuple in the same representation;
@@ -88,7 +88,15 @@ Most Elixir code has a straight-forward translation to its underlying quoted exp
88
88
89
89
Quote is about retrieving the inner representation of some particular chunk of code. However, sometimes it may be necessary to inject some other particular chunk of code inside the representation we want to retrieve.
90
90
91
-
For example, imagine you have a variable `number` which contains the number you want to inject inside a quoted expression. The number can be injected into the quoted representation by using `unquote`:
91
+
For example, imagine you have a variable `number` which contains the number you want to inject inside a quoted expression.
92
+
93
+
```iex
94
+
iex> number = 13
95
+
iex> Macro.to_string(quote do: 11 + number)
96
+
"11 + number"
97
+
```
98
+
99
+
That's not what we wanted, since the value of the `number` variable has not been injected and `number` has been quoted in the expression. In order to inject the *value* of the `number` variable, `unquote` has to be used inside the quoted representation:
0 commit comments