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
Rephrased some sentences and fixed some grammar in ch. 19
- Rephrased some sentences (mainly to fix inconsistent addressing: "to do
that, YOU can do this [...] and so WE need to ...")
- Polished some of the code samples
Copy file name to clipboardExpand all lines: getting_started/19.markdown
+38-27Lines changed: 38 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,18 +8,18 @@ guide: 19
8
8
9
9
{% include toc.html %}
10
10
11
-
We have already learned Elixir provides double-quoted strings and single-quoted char lists. However, this only covers the surface of structures that have textual representation in the language. Atoms are, for example, another structure which are mostly created via the `:atom` representation.
11
+
We have already learned that Elixir provides double-quoted strings and single-quoted char lists. However, this only covers the surface of structures that have textual representation in the language. Atoms are, for example, are mostly created via the `:atom` representation.
12
12
13
-
One of Elixir's goals is extensibility: developers should be able to extend the language to particular domains. Computer science has become such a wide field that it is impossible for a language to tackle many fields as part of its core. Our best bet is to rather make the language extensible, so developers, companies and communities can extend the language to their relevant domains.
13
+
One of Elixir's goals is extensibility: developers should be able to extend the language to fit any particular domain. Computer science has become such a wide field that it is impossible for a language to tackle many fields as part of its core. Our best bet is to rather make the language extensible, so developers, companies and communities can extend the language to their relevant domains.
14
14
15
-
In the chapter, we are going to explore sigils, which are one of the mechanisms provided by the language for working with textual representations.
15
+
In this chapter, we are going to explore sigils, which are one of the mechanisms provided by the language for working with textual representations. Sigils start with the tilde (`~`) character which is followed by a letter (which identifies the sigil) and then a delimiter; optionally, modifiers can be added after the final delimiter.
16
16
17
17
## 19.1 Regular expressions
18
18
19
-
Sigils start with the tilde (`~`) character which is followed by a letter and then a separator. The most common sigil in Elixir is `~r` for[regular expressions](https://en.wikipedia.org/wiki/Regular_Expressions):
19
+
The most common sigil in Elixir is `~r`, which is used to create[regular expressions](https://en.wikipedia.org/wiki/Regular_Expressions):
20
20
21
21
```iex
22
-
# A regular expression that returns true if the text has foo or bar
22
+
# A regular expression that matches strings which contain "foo" or "bar":
23
23
iex> regex = ~r/foo|bar/
24
24
~r/foo|bar/
25
25
iex> "foo" =~ regex
@@ -39,7 +39,7 @@ true
39
39
40
40
Check out the [`Regex` module](/docs/stable/elixir/Regex.html) for more information on other modifiers and the supported operations with regular expressions.
41
41
42
-
So far, all examples have used `/` to delimit a regular expression. However sigils support 8 different separators:
42
+
So far, all examples have used `/` to delimit a regular expression. However sigils support 8 different delimiters:
43
43
44
44
```
45
45
~r/hello/
@@ -52,44 +52,52 @@ So far, all examples have used `/` to delimit a regular expression. However sigi
52
52
~r<hello>
53
53
```
54
54
55
-
The reasoning in supporting different operators is that different separators can be more convenient to different sigils. For example, using parentheses for regular expressions may be a confusing choice as they can get mixed with the parentheses inside the regex. However, parentheses can be handy for other sigils, as we will see in the next section.
55
+
The reason behind supporting different delimiters is that different delimiters can be more suited for different sigils. For example, using parentheses for regular expressions may be a confusing choice as they can get mixed with the parentheses inside the regex. However, parentheses can be handy for other sigils, as we will see in the next section.
56
56
57
57
## 19.2 Strings, char lists and words sigils
58
58
59
59
Besides regular expressions, Elixir ships with three other sigils.
60
60
61
-
The `~s` sigil is used to generate strings, similar to double quotes:
61
+
### 19.2.1 Strings
62
+
63
+
The `~s` sigil is used to generate strings, like double quotes are. The `~s` sigil is useful, for example, when a string contains both double and single quotes:
62
64
63
65
```iex
64
-
iex> ~s(this is a string with "quotes")
65
-
"this is a string with \"quotes\""
66
+
iex> ~s(this is a string with "double" quotes, not 'single' ones)
67
+
"this is a string with \"double\" quotes, not 'single' ones"
66
68
```
67
69
68
-
While `~c` is used to generate char lists:
70
+
### 19.2.2 Char lists
71
+
72
+
The `~c` sigil is used to generate char lists:
69
73
70
74
```iex
71
-
iex> ~c(this is a string with "quotes")
72
-
'this is a string with "quotes"'
75
+
iex> ~c(this is a char list containing 'single quotes')
76
+
'this is a char list containing \'single quotes\''
73
77
```
74
78
75
-
The `~w` sigil is used to generate a list of words separated by white space:
79
+
### 19.2.3 Word lists
80
+
81
+
The `~w` sigil is used to generate lists of words (*words* are just regular strings). Inside the `~w` sigil, words are separated by whitespace.
76
82
77
83
```iex
78
84
iex> ~w(foo bar bat)
79
85
["foo", "bar", "bat"]
80
86
```
81
87
82
-
The `~w` sigil also accepts the `c`, `s` and `a` modifiers (for char lists, strings and atoms, respectively) to choose the format of the result:
88
+
The `~w` sigil also accepts the `c`, `s` and `a` modifiers (for char lists, strings and atoms, respectively) which specify the data type of the elements of the resulting list:
83
89
84
90
```iex
85
91
iex> ~w(foo bar bat)a
86
92
[:foo, :bar, :bat]
87
93
```
88
94
89
-
Besides lowercase sigils, Elixir supports uppercase sigils. While both `~s` and `~S` will return strings, the first one allows escape codes and interpolation while the second does not:
95
+
## 19.3 Interpolation and escaping in sigils
96
+
97
+
Besides lowercase sigils, Elixir supports uppercase sigils to deal with escaping characters and interpolation. While both `~s` and `~S` will return strings, the former allows escape codes and interpolation while the latter does not:
90
98
91
99
```elixir
92
-
iex>~s(String with escape codes \x26interpolation)
100
+
iex>~s(String with escape codes \x26#{"inter"<>"polation"})
93
101
"String with escape codes & interpolation"
94
102
iex>~S(String without escape codes and without #{interpolation})
95
103
"String without escape codes and without \#{interpolation}"
@@ -111,10 +119,10 @@ The following escape codes can be used in strings and char lists:
111
119
*`\t` – tab
112
120
*`\v` – vertical tab
113
121
*`\0` - null byte
114
-
*`\xDD` - character with hexadecimal representation DD (example:`\x13`)
115
-
*`\x{D...}` - character with hexadecimal representation with one or more hexadecimal digits (example:`\x{abc13}`)
122
+
*`\xDD` - character with hexadecimal representation DD (e.g.,`\x13`)
123
+
*`\x{D...}` - character with hexadecimal representation with one or more hexadecimal digits (e.g.,`\x{abc13}`)
116
124
117
-
Sigils also support heredocs, which is when triple double- or single-quotes are used as separators:
125
+
Sigils also support heredocs, that is, triple double- or single-quotes as separators:
118
126
119
127
```iex
120
128
iex> ~s"""
@@ -123,7 +131,7 @@ iex> ~s"""
123
131
...> """
124
132
```
125
133
126
-
The most common case for heredoc sigils is when writing documentation. For example, if you need to write escape characters in your documentation, it can become error prone as we would need to double-escape some characters:
134
+
The most common use case for heredoc sigils is when writing documentation. For example, writing escape characters in documentation would soon become error prone because of the need to double-escape some characters:
127
135
128
136
```elixir
129
137
@doc """
@@ -138,7 +146,7 @@ Converts double-quotes to single-quotes.
138
146
defconvert(...)
139
147
```
140
148
141
-
By using using `~S`, we can avoid this problem altogether:
149
+
By using using `~S`, this problem can be avoided altogether:
142
150
143
151
```elixir
144
152
@doc ~S"""
@@ -153,31 +161,34 @@ Converts double-quotes to single-quotes.
153
161
defconvert(...)
154
162
```
155
163
156
-
## 19.3 Custom sigils
164
+
## 19.4 Custom sigils
157
165
158
-
As hinted at the beginning of this chapter, sigils inElixir are extensible. In fact, the sigil `~r/foo/i` is equivalent to calling the `sigil_r` function with a binary and a char list as argument:
166
+
As hinted at the beginning of this chapter, sigils inElixir are extensible. In fact, using the sigil `~r/foo/i` is equivalent to calling the `sigil_r` function with a binary and a char list as argument:
159
167
160
168
```iex
161
169
iex>sigil_r(<<"foo">>, 'i')
162
170
~r"foo"i
163
171
```
164
172
165
-
That said, we can access the documentation for the `~r` sigil via the `sigil_r` function:
173
+
We can access the documentation for the `~r` sigil via the `sigil_r` function:
166
174
167
175
```iex
168
176
iex> h sigil_r
169
177
...
170
178
```
171
179
172
-
We can also provide our own sigils by simply implementing the proper function. For example, let's implement the `~i(13)` sigil that returns an integer:
180
+
We can also provide our own sigils by simply implementing functions that follow the `sigil_{identifier}` pattern. For example, let's implement the `~i` sigil that returns an integer (with the optional `n` modifier to make it negative):
Sigils can also be used to do compile-time work with the help of macros. For example, regular expressions in Elixir are compiled into efficient representation during compilation of the source code, therefore skipping this step at runtime. If you have interest in the subject, we recommend you to learn more about macros and check how those sigils are implemented in the `Kernel` module.
194
+
Sigils can also be used to do compile-time work with the help of macros. For example, regular expressions in Elixir are compiled into an efficient representation during compilation of the source code, therefore skipping this step at runtime. If you're interested in the subject, we recommend you to learn more about macros and check out how sigils are implemented in the `Kernel` module (where the `sigil_*` functions are defined).
0 commit comments