Implement Sigil in Elixir

Sigil’s in Elixir is an extremely powerful construct which contributes greatly to developer productivity.

Some of the well know sigils are

Regex
Date
Time

But have you ever wondered how you can implement your own sigil?

Recently, José Valim held a live stream on Twitch where he solved Day 4 of Advent of Code. At the end of his stream (in the section he called it his “ramblings”) he explained how sigils can be implemented.

This article is so that I get to practice his teachings by applying them on a chess game I’ve been working on.

Context

Let’s imagine that we have the following struct to represent a chess board with all it’s pieces

As can be seen in the setup functions, it would be difficult to initialize a chess board using all of it’s pieces.

The easy way around this would be by using a sigil.

Implementation

Once you’ve implemented the sigil_ macro you will be able to initialize a new chess board by using the following

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store