# Quoting symbols

**URL:** <https://discourse.julialang.org/t/quoting-symbols/23308>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [April 19, 2019, 4:25pm UTC](https://discourse.julialang.org/t/quoting-symbols/23308 "2019-04-19T16:25:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [April 19, 2019, 4:25pm UTC](https://discourse.julialang.org/t/quoting-symbols/23308/1 "2019-04-19T16:25:16Z")

</div>

This seems like it should be simple, but it’s still tripping me up. I need to write a function that gives this result:

```julia
julia> f(:x)
:(someArray[:x])

```

Interpolation gives me a “raw” x, and I’m having a surprisingly hard time making a Symbol of it.

On a side note, it seems like this kind of thing happens a lot in metaprogramming. Something that seems like it ought to be simple just… isn’t. Is this just me? Or metaprogramming in Julia? Or something about metaprogramming in general?

I’ve read that in general macros aren’t composable, but in this case there aren’t any macros involved. Do we need more (or better) tools or combinators for this stuff? Or maybe a way to build the right mental model?

Julia is already much easier than it would be to produce similar results in something like Python. So it feels like I’m making progress and getting quicker at it. It’s just a bit frustrating for simple things like this to be throwing me.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 19, 2019, 4:33pm UTC](https://discourse.julialang.org/t/quoting-symbols/23308/2 "2019-04-19T16:33:41Z")

</div>

You want `QuoteNode`: `f(s) = :(someArray[$(QuoteNode(s))])`

When in doubt, just `dump` an expression you would like to create and see what it is made of:

```julia
julia> dump(:(someArray[:x]))
Expr
  head: Symbol ref
  args: Array{Any}((2,))
    1: Symbol someArray
    2: QuoteNode
      value: Symbol x

```

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [April 19, 2019, 4:42pm UTC](https://discourse.julialang.org/t/quoting-symbols/23308/3 "2019-04-19T16:42:51Z")

</div>

> [@stevengj](#):
>
> You want `QuoteNode`

That’s it, thanks!

> [@stevengj](#):
>
> When in doubt, just `dump`

Yep, I should have thought of that. There’s not much documentation on `QuoteNode`. Is this its primary use? Any other common use cases?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [April 19, 2019, 4:45pm UTC](https://discourse.julialang.org/t/quoting-symbols/23308/4 "2019-04-19T16:45:54Z")

</div>

Something that I’ve always found very confusing is that there is `QuoteNode(x)`, but there is also `Expr(:quote, x)`. As far as I can tell these two are functionally equivalent, even though they have completely different data types. I would like some clarification of why `QuoteNode` exists as well.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 19, 2019, 4:56pm UTC](https://discourse.julialang.org/t/quoting-symbols/23308/5 "2019-04-19T16:56:38Z")

</div>

And there’s `Meta.quot`…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 19, 2019, 4:58pm UTC](https://discourse.julialang.org/t/quoting-symbols/23308/6 "2019-04-19T16:58:32Z")

</div>

I believe `QuoteNode` is documented in the dev doc (on my phone, didn’t check)

The two are not equivalent. The `Expr(:quote)` support interpolation expression when evaluated. `QuoteNode` simply return the quoted value. It’s like the difference between the quote and backquote in lisp.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [April 19, 2019, 4:59pm UTC](https://discourse.julialang.org/t/quoting-symbols/23308/7 "2019-04-19T16:59:10Z")

</div>

`Meta.quot` is just a function to create the expr iirc.
