Skip to content

Commit

Permalink
Remove duplicate sigil docs now that guides are included
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 4, 2023
1 parent 61b5aaf commit f9cca8f
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 70 deletions.
69 changes: 0 additions & 69 deletions lib/elixir/lib/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,75 +61,6 @@ defmodule Macro do
> The functions in this module do not evaluate code. In fact,
> evaluating code from macros is often an anti-pattern. For code
> evaluation, see the `Code` module.
## Custom Sigils
Macros are also commonly used to implement custom sigils.
Sigils start with `~` and are followed by one lowercase letter or by one
or more uppercase letters, and then a separator
(see the [Syntax Reference](syntax-reference.md)). One example is
`~D[2020-10-13]` to define a date.
To create a custom sigil, define a macro with the name `sigil_{identifier}`
that takes two arguments. The first argument will be the string, the second
will be a charlist containing any modifiers. If the sigil is lower case
(such as `sigil_x`) then the string argument will allow interpolation.
If the sigil is one or more upper case letters (such as `sigil_X` and
`sigil_EXAMPLE`) then the string will not be interpolated.
Valid modifiers are ASCII letters and digits. Any other character will
cause a syntax error.
Single-letter sigils are typically reserved to the language. Multi-letter
sigils are uppercased and extensively used by the community to embed
alternative markups and data-types within Elixir source code.
The module containing the custom sigil must be imported before the sigil
syntax can be used.
### Examples
As an example, let's define a sigil `~x` and sigil `~X` which
return its contents as a string. However, if the `r` modifier
is given, it reverses the string instead:
defmodule MySigils do
defmacro sigil_x(term, [?r]) do
quote do
unquote(term) |> String.reverse()
end
end
defmacro sigil_x(term, _modifiers) do
term
end
defmacro sigil_X(term, [?r]) do
quote do
unquote(term) |> String.reverse()
end
end
defmacro sigil_X(term, _modifiers) do
term
end
end
import MySigils
~x(with #{"inter" <> "polation"})
#=> "with interpolation"
~x(with #{"inter" <> "polation"})r
#=> "noitalopretni htiw"
~X(without #{"interpolation"})
#=> "without \#{"interpolation"}"
~X(without #{"interpolation"})r
#=> "}\"noitalopretni\"{# tuohtiw"
"""

alias Code.Identifier
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/pages/getting-started/sigils.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,4 @@ iex> ~i(42)n
Custom sigils may be either a single lowercase character or several uppercase characters.
Sigils can also be used to do compile-time work with the help of macros. For example, regular expressions in Elixir are compiled into an efficient representation during compilation of the source code, therefore skipping this step at runtime. If you're interested in the subject, we recommend you learn more about macros and check out how sigils are implemented in the `Kernel` module (where the `sigil_*` functions are defined).
Sigils can also be used to do compile-time work with the help of macros. For example, regular expressions in Elixir are compiled into an efficient representation during compilation of the source code, therefore skipping this step at runtime. If you're interested in the subject, you can learn more about macros and check out how sigils are implemented in the `Kernel` module (where the `sigil_*` functions are defined).

0 comments on commit f9cca8f

Please sign in to comment.