Sigils are an alternate syntax for certain literals. A sigil starts with a tilde (~) followed by an upper- or lower-case letter, followed by some content surrounded by delimiters.
For example, the string "This is a string"
can be represented by the double-quoted string literal or by the ~s
sigil.
iex> ~s(This is a string)
"This is a string"
Note that in the above example, we used a set of parentheses as the delimiters. The string contents were between the delimiters. ParentheseS are the most common delimiters that I've seen, but they aren't the only deleimiters. There are different sets of delimiters that can be substituted: <>
, {}
, []
. ||
, //
, ""
, and ` `
So if we want to have parentheses in our string, we can use an alternate set of delimiters
iex> ~s{This is a string(with parentheses)}
"This is a string(with parentheses)"
iex> ~s|This is a string(with parentheses)|
"This is a string(with parentheses)"
iex> ~s"This is a string(with parentheses)"
"This is a string(with parentheses)"
I could also use any other set of delimiters from the list above.
This means that you can use a string sigil to have double quotes in your string without having to escape them, as long as you avoid the double quote delimiters.
The string sigil is also available as an alternate version with an upper-case "S", where there is no interpolation or escaping like with a standard string literal. This makes it easier to have escape sequences and interpolation syntax in a string.
iex> ~s<This is an \s escaped string with #{1 + 1} interpolation>
"This is an escaped string with 2 interpolation"
iex> ~S<This is an \s escaped string with #{1 + 1} interpolation>
"This is an \\s escaped string with \#{1 + 1} interpolation"
It looks like the ~S
sigil actually added escape sequences, but that's not the case. IEx just chooses to display those characters as escape sequences because that's how they appear in string literals.
Here are the list of sigils in Elixir
~C
a character list without escaping or interpolation~c
a character list with escaping and interpolation~D
a Date literal (yyyy-mm-dd)~N
a naive DateTime literal (yyyy-mm-dd hh:mm:ss)~R
a regular expression without escaping or interpolation~r
a regular expression with escaping and interpolation~S
a string without escaping or interpolation~s
a string with escaping and interpolation~T
a Time literal (hh:mm:ss)~W
a word list, with no escaping or interpolation~w
a word list with escaping and interpolation
The only sigils I think need more explanation at this point are the word list sigils, ~w
and ~W
.
By default, a word list is a list of strings.
iex> ~w(This is a word list)
["This", "is", "a", "word", "list"]
The word list sigils also have options that go on the end that indicate the type of list to be generated. The "a" option causes a list of atoms to be generated, the "c" option causes a character list to be generated, and the "s" option causes a list of strings to be generated.
iex> ~w(This is a word list)a
[:This, :is, :a, :word, :list]
iex> ~w(This is a word list)c
['This', 'is', 'a', 'word', 'list']
iex> ~w(This is a word list)s
["This", "is", "a", "word", "list"]
I've read that it is possible to define your own sigils, but I haven't learned how to do that yet.