June 2022
Intermediate to advanced
130 pages
2h 45m
English
Elixir developers must deal with many different data types with different representations. Sigils exist to make text representations of data types less awkward. Let’s look at an example.
Every programmer knows it’s hard to read code with strings containing quotes. In Elixir, you’d represent quotes within a string with a preceding backslash, like this:
| | IO.puts "\"string with quotes\"" |
| | "string with quotes" |
| | :ok |
Now, how would you represent the first line in the previous listing as a string? You would need to escape every quote and backslash! Or, you could use a sigil, like this:
| | iex> ~s("string with quotes") |
| | "\"string with quotes\"" |
~s is a sigil. The alternative syntax for strings without quotes means we don’t have to ...
Read now
Unlock full access