# Advanced Metaprogramming Uses of QuoteNode

**URL:** <https://discourse.julialang.org/t/advanced-metaprogramming-uses-of-quotenode/110708>\
**Category:** General Usage\
**Tags:** macros, metaprogramming\
**Created:** [February 24, 2024, 7:56pm UTC](https://discourse.julialang.org/t/advanced-metaprogramming-uses-of-quotenode/110708 "2024-02-24T19:56:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [February 24, 2024, 7:56pm UTC](https://discourse.julialang.org/t/advanced-metaprogramming-uses-of-quotenode/110708/1 "2024-02-24T19:56:58Z")

</div>

Continuing the discussion from [Is there a better way to write this macro?](https://discourse.julialang.org/t/is-there-a-better-way-to-write-this-macro/110610/37):

> [@Is there a better way to write this macro?](https://discourse.julialang.org/t/is-there-a-better-way-to-write-this-macro/110610/37):
>
> Welp. I’m still not sure what `QuoteNode` is good for, but nevertheless, the macro now produces them. If I ever come up with an application for this, I’ll let y’all know right away.

Quoth [The Manual](https://docs.julialang.org/en/v1/manual/metaprogramming/#man-quote-node):

> The parser yields `QuoteNode`s for simple quoted items like symbols.  
> `QuoteNode` can also be used for certain advanced metaprogramming tasks.

Has anyone encountered one of these advanced metaprogramming tasks? A `QuoteNode` symbol is very easy to encounter, of course:

```julia
julia> dump(:(:quoted))
QuoteNode
  value: Symbol quoted

```

And the manual shows a method for constructing a `$` expression which won’t interpolate when evaluated.

Are there cases in Julia’s source code where QuoteNodes get used this way? Anyone out there had a use for this technique in the wild that they’d like to share?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 24, 2024, 10:28pm UTC](https://discourse.julialang.org/t/advanced-metaprogramming-uses-of-quotenode/110708/2 "2024-02-24T22:28:37Z")

</div>

The `Quotenode` section is easier to understand when you keep in mind the preceding section on nested quotes. It may also help to point out that the counterexample in the `QuoteNode` section is `Meta.quot(Expr(:$, :(1+2))) == :(:($(1+2)))`. That makes it easier to see that when evaluated, the outer quote layer is stripped away to `:($(1+2))`, but not really because it also interpolates to `:(3) == 3`. A better example would probably be `:(identity( :($(1+2)) ))`, where it’s more apparent how fast that interpolation happens when you evaluate functions that work on expressions. In the vast majority of metaprogramming cases, you want the input expression to evaluate along with the rest of the output expression, but `QuoteNode` becomes useful when you are forwarding the input expression to a function working on expressions inside the output expression, really puts the meta in metaprogramming.

For an example, the `macroexpand` and `macroexpand1` macros manually call a `Quotenode` before interpolating it into a `macroexpand` function call expression. Take `@macroexpand @eval $(1 + 2)`: the input expression for the `macroexpand` macro is `Expr(:macrocall, Symbol("@eval"), :(#=...=#), Expr(:$, Expr(:call, :+, 1, 2)))`, so far so good. You only want the `@eval` call to handle the interpolation in `Expr(:$, Expr(:call, :+, 1, 2))`, not let it occur when the `macroexpand` function call expression is evaluated.
