Constructing expressions with local macro variables and Symbols

Let me first say that I fully support what @jling said about macros not being composable, and that I would advise you to try and avoid having to use macros in this case.

That being said, as you found out, QuoteNode is the key here, as it allows you to build a quoted symbol (and is the only way to do so AFAIK). But you can perfectly use it in an otherwise standard quoted expression.
Your original solution could therefore be reduced to something like:

julia> macro mymacro(ex)
           name, idx = ex.args
           return :([[ $(QuoteNode(name)) ], $(idx) ])
       end
@mymacro (macro with 1 method)

julia> @macroexpand @mymacro mysym[15]
:([[:mysym], 15])

which I don’t find too unintuitive (once you know about QuoteNode)

2 Likes