Funny Characters in Elixir: Sigils

There are a number of special character sequences in Elixir, for example the pipeline operator |> but now I want to discuss the ~something options.

These are sigils. The magic is simple if you call ~x(“something”) then the compiler will translate this into a call to sigil_x

~r/foo/i becomes sigil_r(<<"foo">>, 'i')

See for more details.

This does mean that you can now write your own sigils.

Here are the built in sigils. If there is an uppercase/lowercase pair they work the same except the lowercase one escapes the content.

sigil_C/2          sigil_c/2 returns a charlist

sigil_D/2          Creates date types

sigil_N/2         Creates native datetime

sigil_R/2          sigil_r/2        Regular expressions  

sigil_S/2          sigil_s/2          Creates strings from literals

sigil_T/2          Creates time types

sigil_U/2          Creates a UTC Datetime

sigil_W/2          sigil_w/2          Creates a list of words by splitting on whitespace



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s