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 - it's based on our experience of managing over 50-people strong company. The tool we're missing as a small company and not an enterprise.

humadroid.io is an employee and performance management software. It's an unique tool allowing everyone to be in the loop - by having up to date info about co-workers, time-off, benefits, assets, helping with one-on-ones, being a go-to place for company-wide announcements.

Check out 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!