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
Update getting started charlists to use ~c (#1702)
* Update getting started charlists to use ~c
* Update getting-started/basic-types.markdown
Co-authored-by: José Valim <[email protected]>
---------
Co-authored-by: José Valim <[email protected]>
Copy file name to clipboardExpand all lines: getting-started/basic-types.markdown
+13-4
Original file line number
Diff line number
Diff line change
@@ -320,21 +320,28 @@ iex> hd([])
320
320
** (ArgumentError) argument error
321
321
```
322
322
323
-
Sometimes you will create a list and it will return a value in single quotes. For example:
323
+
Sometimes you will create a list and it will return a quoted value preceded by `~c`. For example:
324
324
325
325
```elixir
326
326
iex> [11, 12, 13]
327
-
'\v\f\r'
327
+
~c"\v\f\r"
328
+
iex> [104, 101, 108, 108, 111]
329
+
~c"hello"
330
+
```
331
+
332
+
In Elixir versions before v1.15, this might be displayed as single quotes instead:
333
+
334
+
```elixir
328
335
iex> [104, 101, 108, 108, 111]
329
336
'hello'
330
337
```
331
338
332
339
When Elixir sees a list of printable ASCII numbers, Elixir will print that as a charlist (literally a list of characters). Charlists are quite common when interfacing with existing Erlang code. Whenever you see a value in IEx and you are not quite sure what it is, you can use the `i/1` to retrieve information about it:
333
340
334
341
```elixir
335
-
iex> i 'hello'
342
+
iex> i ~c"hello"
336
343
Term
337
-
'hello'
344
+
i ~c"hello"
338
345
Data type
339
346
List
340
347
Description
@@ -352,6 +359,8 @@ Keep in mind single-quoted and double-quoted representations are not equivalent
352
359
```elixir
353
360
iex> 'hello' == "hello"
354
361
false
362
+
iex> 'hello' == ~c"hello"
363
+
true
355
364
```
356
365
357
366
Single quotes are charlists, double quotes are strings. We will talk more about them in the ["Binaries, strings and charlists"](/getting-started/binaries-strings-and-char-lists.html) chapter.
Copy file name to clipboardExpand all lines: getting-started/binaries-strings-and-char-lists.markdown
+21-12
Original file line number
Diff line number
Diff line change
@@ -238,39 +238,48 @@ Our tour of our bitstrings, binaries, and strings is nearly complete, but we hav
238
238
239
239
**A charlist is a list of integers where all the integers are valid code points.** In practice, you will not come across them often, only in specific scenarios such as interfacing with older Erlang libraries that do not accept binaries as arguments.
Charlists used to be represented with single quotes in Elixir <1.15:
242
253
243
254
```elixir
244
255
iex>'hello'
245
-
'hello'
246
-
iex> [?h, ?e, ?l, ?l, ?o]
247
-
'hello'
256
+
~c"hello"
248
257
```
249
258
250
259
The key takeaway is that `"hello"` is not the same as `'hello'`. Generally speaking, **double-quotes must always be used to represent strings in Elixir**. In any case, let's learn how charlists work.
251
260
252
261
Instead of containing bytes, a charlist contains integer code points. However, the list is only printed in single-quotes if all code points are within the ASCII range:
253
262
254
263
```elixir
255
-
iex>'hełło'
264
+
iex>~c"hełło"
256
265
[104, 101, 322, 322, 111]
257
-
iex>is_list('hełło')
266
+
iex>is_list(~c"hełło")
258
267
true
259
268
```
260
269
261
270
Interpreting integers as code points may lead to some surprising behavior. For example, if you are storing a list of integers that happen to range between 0 and 127, by default IEx will interpret this as a charlist and it will display the corresponding ASCII characters.
262
271
263
272
```elixir
264
273
iex> heartbeats_per_minute = [99, 97, 116]
265
-
'cat'
274
+
~c"cat"
266
275
```
267
276
268
277
You can convert a charlist to a string and back by using the `to_string/1` and `to_charlist/1` functions:
269
278
270
279
```elixir
271
280
iex>to_charlist("hełło")
272
281
[104, 101, 322, 322, 111]
273
-
iex>to_string('hełło')
282
+
iex>to_string(~c"hełło")
274
283
"hełło"
275
284
iex>to_string(:hello)
276
285
"hello"
@@ -283,14 +292,14 @@ Note that those functions are polymorphic - not only do they convert charlists t
283
292
String (binary) concatenation uses the `<>` operator but charlists, being lists, use the list concatenation operator `++`:
284
293
285
294
```elixir
286
-
iex>'this '<>'fails'
287
-
** (ArgumentError) expected binary argument in<> operator but got:'this '
295
+
iex>~c"this "<>~c"fails"
296
+
** (ArgumentError) expected binary argument in<> operator but got:~c"this "
Copy file name to clipboardExpand all lines: getting-started/comprehensions.markdown
+1-1
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ Comprehensions discard all elements for which the filter expression returns `fal
44
44
Comprehensions generally provide a much more concise representation than using the equivalent functions from the `Enum` and `Stream` modules. Furthermore, comprehensions also allow multiple generators and filters to be given. Here is an example that receives a list of directories and gets the size of each file in those directories:
Copy file name to clipboardExpand all lines: getting-started/sigils.markdown
+6-4
Original file line number
Diff line number
Diff line change
@@ -65,11 +65,13 @@ iex> ~s(this is a string with "double" quotes, not 'single' ones)
65
65
66
66
### Char lists
67
67
68
-
The `~c` sigil is useful for generating char lists that contain single quotes:
68
+
The `~c` sigil is the regular way to represent charlists.
69
69
70
70
```elixir
71
-
iex>~c(this is a char list containing 'single quotes')
72
-
'this is a char list containing \'single quotes\''
71
+
iex> [?c, ?a, ?t]
72
+
~c"cat"
73
+
iex>~c(this is a char list containing "double quotes")
74
+
~c"this is a char list containing \"double quotes\""
73
75
```
74
76
75
77
### Word lists
@@ -214,7 +216,7 @@ iex> time_zone
214
216
As hinted at the beginning of this chapter, sigils inElixir are extensible. In fact, using the sigil `~r/foo/i` is equivalent to calling `sigil_r` with a binary and a char list as the argument:
0 commit comments