Want to learn?

Sigils

Do you remember that one of the reasons for creating Elixir was bringing extensibility to Erlang? In this chapter, you will learn how Sigils brings extensibility to textual representations.

Representation

Documentation

As already mentioned, sigil is a mechanism in Elixir for working with textual representations. Every sigil starts with a tilde (~) which is followed by a letter (this is an identification of a sigil) and delimiter (any nonalphanumeric character). Here is a syntax of sigil:

~ + letter + delimiter

For example:
~r/foobar/
~s(hello world)

Extensibility

Sigils are great because of their extensibility, but what does it actually mean? It turns out you can create your own custom sigil with functions (you'll learn soon about functions in Elixir). Since we work on textual representations here, any sigil function must be given a string as the first argument and set of not obligatory options as the second one.

iex> defmodule MySigils do
...> def sigil_i(string, []), do: String.to_integer(string)
...> def sigil_i(string, [?n]), do: -String.to_integer(string)
...> end
iex> import MySigils
iex> ~i(13)
13
iex> ~i(42)n
-42

In this example, we declare sigil responsible for converting a string to an integer, but you can transform such a string in any way you want. That's why sigils are useful. Whenever you identify repetitive tasks performed on a string in your project, you should consider creating sigil out of it. It can speed up the development process and refactor your code in a clean way.

Check our latest product - built from our experience helping growing businesses navigate complex compliance requirements without enterprise budgets.

humadroid.io is an affordable, all-in-one GRC platform designed for small and medium-sized businesses pursuing SOC 2 or ISO 27001 compliance. Our AI-powered compliance assistant understands your business context and transforms complex compliance work into actionable steps - generating tailored policy documentation in minutes instead of weeks, and helping draft your SOC 2 System Description in a fraction of the usual time. At just $250/month with no hidden fees or user limits, customers save 10-15 hours per week on compliance work.

Explore humadroid.io
Top

Contact us

* Required fields

The controller of your personal data provided via this contact form is Prograils sp. z o.o., with a registered seat at Sczanieckiej 9A/10, 60-215 Poznań. Your personal data will be processed in order to respond to your inquiries and for our marketing purposes (e.g. when you ask us for our post-development, maintenance or ad hoc engagements for your app). You have the rights to: access your personal data, rectify or erase your personal data, restrict the processing of your personal data, data portability and to object to the processing of your personal data. Learn more.

Notice

We do not track you online. We use only session cookies and anonymous identifiers for the purposes specified in the cookie policy. No third-party trackers.

I understand
Elo Mordo!Elo Mordo!