amalbuquerque
Hi there,
After reading @ericmj response here (Using unquote outside of quote block - #6 by ericmj) about “unquote fragments”, I still can’t completely grasp the concept.
I’ve read the documentation many times now, besides some other resources (including McCord’s Metaprogramming Elixir), and I can’t reach to a concrete definition of what an unquote fragment is.
I’ve read in some places that “unquote fragments” is the usage of unquote that lets us dynamically define functions and nested macros (The minimum knowledge you need to start Metaprogramming in Elixir - DockYard). After reading all this, I think both unquote/1 calls on the documentation example are “unquote fragments”, since they will be injected on the caller context and won’t have an explicit quote block enclosing them. Am I right?:
defmacro defkv(kv) do
quote bind_quoted: [kv: kv] do
Enum.each kv, fn {k, v} ->
def unquote(k)(), do: unquote(v)
end
end
end
And consider this example please:
defmodule Foo do
alias Baz
fun_definition = {:bar, [], [{:arg, [], nil}]}
body = {:__block__, [], [{:*, [], [{:arg, [], nil}, {:arg, [], nil}]}]}
function = :bar
arity = 1
def(unquote(fun_definition)) do
mfa = Baz.mfa(__MODULE__, unquote(function), unquote(arity)) # returns "Module.function/arity"
Baz.run(mfa, fn -> unquote(body) end)
end
end
Here I think the unquote(fun_definition) is definitely an unquote fragment because is allowing us to dynamically define the function. But I am not so sure regarding the other unquote/1 calls for function, arity and body. Since they are contributing for the dynamic definition of the function body, are they also unquote fragments?
Thanks in advance,
And thank you all for the amazing community!
lejboua
P.S.: Given the different opinions and definitions I’ve read, IMO the documentation could be updated to include an explicit unquote fragment definition. This way those like me who are starting to look at Elixir would be able to clearly distinguish between a “normal” unquote inside an explicit quote block and an unquote fragment.
P.S.2: I know that we can use the unquote/1 calls inside the def/2 macro arguments because the def/2 arguments, like any macro, are being quoted for us.
Many thanks!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
I think this post has a good summary: Using unquote outside of quote block
amalbuquerque
Hi @kip, thanks for the reply. I’ve mentioned that post on my initial message. I understand why we can do it, it may seem at first glance there’s no
quoteblock wrapping theunquotecall, but there is one in fact due to the macro arguments being quoted for us.What I didn’t understand and think it isn’t completely clear on the documentation is the concept of
unquote fragment. Saying that it is what we use to dynamically define a function IMO gives room for interpretation.adamu
I’m also curious about this.
:foo()is not valid elixir.mudasobwa
That’s how
quote/2works, it simply quotes terms without further digging in. Maybe the example withunquote: falsewould shed some light.So the former results in
:fooand the latter results in the plain AST triple with:foocoming fromunquote(:foo)and the rest coming from parentheses. It’s likewhere
:+is the top-level AST term;()become a top-level AST term in your last example, but parentheses are a special case and they are becoming{:foo, [] = _meta, [] = _args}instead of literally the same{:(), [] = _meta, [:foo]}.:()notation is not possible because args might be not empty. Technically, it might have been{:(), [], [:foo, []]}, but it would make is harder to walk through.adamu
This
unquote: falseexample is very useful, thank you! It’s makes it easy to see how it’s the parentheses define that it’s a function call, and how:unquote(:foo)()becomes{{:unquote, [], [:foo]}, [], []}As this thread is about unquote fragments, is this what an unquote fragment is? There’s no special treatment, they’re just
unquote/1/{:unquote, ..., ...}calls that’s are left in the AST for later evaluation in context.When we do something like:
This will produce “unquote fragments” for
nameandvaluein the AST:Which are resolved in
Dynamic’s context when thedefmacro is evaluated.