Sigils
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 ...
Get Programmer Passport: Elixir now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.