# Is there a better way to write this macro?

**URL:** <https://discourse.julialang.org/t/is-there-a-better-way-to-write-this-macro/110610>\
**Category:** General Usage\
**Tags:** macros, metaprogramming, eval\
**Created:** [February 22, 2024, 10:59pm UTC](https://discourse.julialang.org/t/is-there-a-better-way-to-write-this-macro/110610 "2024-02-22T22:59:23Z")\
**Posts on this page:** 1\
**Showing post:** 37

<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:09pm UTC](https://discourse.julialang.org/t/is-there-a-better-way-to-write-this-macro/110610/37 "2024-02-24T19:09:51Z")

</div>

So if I double-wrap the QuoteNode like this, one gets through the escaping process:

```julia
return QuoteNode(Expr(:$, QuoteNode(toquote)))

```

That gives the expected (?) behavior:

```julia
julia> @sexpr (:if, (2 < 3), $(QuoteNode(Expr(:call, :f, :x, Expr(:$, :y), :z))))
:(if 2 < 3
      $(QuoteNode(:(f(x, $(Expr(:$, :y)), z))))
  end)

julia> cons(ans, :(f(z,c,z)))
:(if 2 < 3
      $(QuoteNode(:(f(x, $(Expr(:$, :y)), z))))
  else
      f(z, c, z)
  end)

julia> eval(ans)
:(f(x, $(Expr(:$, :y)), z))

julia> eval(ans)
syntax: "$" expression outside quote

julia> @sexpr (:if, (2 < 3), ~(f(x,$y,z)))
:(if 2 < 3
      $(QuoteNode(:(f(x, $(Expr(:$, :y)), z))))
  end)

julia> cons(ans, :(f(z,c,z)))
:(if 2 < 3
      $(QuoteNode(:(f(x, $(Expr(:$, :y)), z))))
  else
      f(z, c, z)
  end)

julia> eval(ans)
:(f(x, $(Expr(:$, :y)), z))

julia> eval(ans)
syntax: "$" expression outside quote

```

Now `~` acts like the manual’s example:

```julia
julia> @sexpr (~($(1+2)))
:($(QuoteNode(:($(Expr(:$, :(1 + 2)))))))

julia> eval(eval(ans))
syntax: "$" expression outside quote

julia> QuoteNode(Expr(:$, :(1+2)))
:($(QuoteNode(:($(Expr(:$, :(1 + 2)))))))

julia> eval(eval(ans))
syntax: "$" expression outside quote

```

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.

---

_[View the full topic](https://discourse.julialang.org/t/is-there-a-better-way-to-write-this-macro/110610)._
