I was reading the page about sigils in the Elixir tutorial.
I expected the syntax for defining sigils uses "defsigil" just like "defstruct", "defprotocol", and so on.
But it was not so.
Why?
Why the syntax for defining sigils in Elixir doesn't use "defsigil"?
435 Views Asked by ryo33 At
2
There are 2 best solutions below
3

Sigils are just a fancy way to call specific methods. Take a look at the section on Custom Sigils. Basically ~x/things/options
is the same as sigil_x(things, options)
. So you can write something like:
defmodule Thing do
def sigil_u(string, _options) do
string |> String.upcase
end
def test do
~u/bob/
end
end
IO.inspect Thing.test
The original sigil syntax was
def __s__
wheres
is the character used for the sigil (this would now bedef sigil_s
.) You can see this in the initial commit that started work on sigils. I believe this work started before macros were implemented.This syntax required a hack to allow them to be imported which you can read about in this issue.
You can see in the discussion that some other suggestions such as
defmodule Sigil.s
were suggested, however thedef sigil_s
syntax was ultimately chosen.The actual commit that implements these changes is https://github.com/elixir-lang/elixir/commit/c6284557e792efd67f13f421b723a7a301bdbb93
I am not sure why it is not
defsigil
perhaps nobody suggested it? This is my best guess given that at the time of this post, searching for "defsigil" on Google only returns this question. If it was mentioned on GitHub or IRC then there would have been a mention of it in the search results.