Skip to content

Commit 05c1630

Browse files
author
José Valim
committed
Add missing unquote/unquote_splicing documentation
1 parent 46fc45d commit 05c1630

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,46 @@ defmodule Kernel.SpecialForms do
651651
"""
652652
defmacro alias!(alias)
653653

654+
@doc """
655+
Unquotes the given expression from inside a macro.
656+
657+
## Examples
658+
659+
Imagine the situation you have a variable `name` and
660+
you want to inject it inside some quote. The first attempt
661+
would be:
662+
663+
value = 13
664+
quote do: sum(1, value, 3)
665+
666+
Which would then return:
667+
668+
{ :sum, [], [1, { :value, [], quoted }, 3] }
669+
670+
Which is not the expected result. For this, we use unquote:
671+
672+
value = 13
673+
quote do: sum(1, unquote(value), 3)
674+
#=> { :sum, [], [1, 13, 3] }
675+
676+
"""
677+
name = :unquote
678+
defmacro unquote(name)(expr)
679+
680+
@doc """
681+
Unquotes the given list expanding its arguments. Similar
682+
to unquote.
683+
684+
## Examples
685+
686+
values = [2,3,4]
687+
quote do: sum(1, unquote_splicing(values), 5)
688+
#=> { :sum, [], [1, 2, 3, 4, 5] }
689+
690+
"""
691+
name = :unquote_splicing
692+
defmacro unquote(name)(expr)
693+
654694
@doc """
655695
List comprehensions allow you to quickly build a list from another list:
656696

0 commit comments

Comments
 (0)