Search⌘ K
AI Features

Writing Our Own Sigils

Explore how to create custom sigils in Elixir, enabling you to extend string and literal syntax. Learn about interpolation, pattern matching on options, and overriding built-in sigils with practical coding examples.

We'll cover the following...

Writing a sigil

Elixir is packed with features that make coding a joy. This chapter contains a smattering of them.

We know by now that we can create strings and regular-expression literals using sigils:

string = ~s{now is the time} 
regex = ~r{..h..}

Have you ever wished you could extend these sigils to add your own specific literal types? You can. When we write a sigil such as ~s{...}, Elixir converts it into a call to the function sigil_s. It passes the function two values:

  • The first is the string between the delimiters.
  • The second is a list
...