Quoting symbols

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> f(: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.

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> dump(:(someArray[:x]))
  head: Symbol ref
  args: Array{Any}((2,))
    1: Symbol someArray
    2: QuoteNode
      value: Symbol x

That’s it, thanks!

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

1 Like

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.


And there’s Meta.quot

1 Like

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.


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