Code templates, quoting, and variable substitution

When you write:

        quote
        function $primitive(f::Function, io::IOBuffer = BUFFER; kwargs...)
          element(f, $primitive, io; kwargs...)
        end

you’re producing code that looks something like:

function svg(f...)
  element(f, svg, ...)
end

where svg is a symbol in the resulting code (just like element, and f are), but not actually a quoted symbol itself.

Or, in simpler form, you’re doing this:

julia> name = :a
:a

julia> quote
         $name
       end
quote
    #= REPL[7]:2 =#
    a
end

when what you want is this:

julia> quote
         $(QuoteNode(name))
       end
quote
    #= REPL[8]:2 =#
    :a
end

I think element(f, $(QuoteNode(primitive)), io; kwargs...) should resolve the issue.

1 Like